Hello I'm trying to upload user photo in my laravel app,
When the user updating his/her profile user can add a propic.
The Problem
Now when I run my current code, If the user trying to update profile without uploading a photo, it gives me error saying
"Call to a member function extension() on null"
But when I try to upload a photo, photo get uploaded successfully but the image name not storing in the DB (null value saved instead of image name) and other fields won't GET UPDATED.
Here is my user profile update controller code (Only the function is included)
public function update(Request $request, User $setting)
{
$changedAttributes = array_diff($request->all(), $setting->getAttributes());
$validationRules = array_intersect_key([
'name' => ['required', 'alpha','min:2', 'max:255'],
'last_name' => ['required', 'alpha','min:5', 'max:255'],
'mobile' => ['required', 'string', 'regex:/\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|
2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|
4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.$setting->id.''],
'propic' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
], $changedAttributes);
$imageName = time().'.'.$request->propic->extension();
$request->propic->move(public_path('propics'), $imageName);
$this->validate($request, $validationRules);
$setting->update($changedAttributes);
return Redirect::back()->with('success',__('sentence.User updated successfully'));
}
And following is my form,
<form action="{{ route('settings.update',$user->id) }}" method="POST" enctype="multipart/form-data" >
<div class="row mt-5">
<div class="col-sm-3">
<img src="/img/blank_user.png" alt="Profile Pic" style="border-radius: 100px;height: 150px;width: 150px; opacity: 0.5;" id="profile_pic_display">
<input type="file" name="propic" class="form-control">
</div>
<div class="col-sm-9">
<!-- <form action="{{ route('settings.update',$user->id) }}" method="POST"> -->
#csrf
#method('PUT')
<div class="row">
<div class="col-md-6">
<div class="form-group field-user-firstname required">
<label class="control-label" for="user-firstname">{{ __('sentence.First Name') }}</label>
<input id="name" type="text" class="form-control #error('name') is-invalid #enderror" name="name" value="{{$user->name}}" required autocomplete="name" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-md-6">
<div class="form-group field-user-lastname required">
<label class="control-label" for="user-lastname">{{ __('sentence.Last Name') }}</label>
<input id="last_name" type="text" class="form-control #error('name') is-invalid #enderror" name="last_name" value="{{$user->last_name}}" required autocomplete="last_name" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group field-user-mobile required">
<label class="control-label" for="user-mobile">{{ __('sentence.Mobile') }}</label>
<input id="mobile" type="text" class="form-control #error('mobile') is-invalid #enderror" name="mobile" value="{{$user->mobile}}" required autocomplete="mobile" autofocus>
#error('mobile')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-md-6">
<div class="form-group field-user-email required">
<label class="control-label" for="user-email">{{ __('sentence.Email') }}</label>
<input id="email_" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{$user->email}}" required autocomplete="email">
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
</div>
<div class="row">
<div class="pull-right form-group">
<button type="submit" class="btn btn-default">{{ __('sentence.Cancel') }}</button>
<button type="submit" class="btn btn-primary">{{ __('sentence.Update') }}</button>
</div>
</div>
</div>
</form>
Try to replace
$imageName = time().'.'.$request->propic->extension();
To these
$imageName = time().'.'.$request->propic->getClientOriginalExtension();
So finally I able to found a solution for my above stated problem,
I changed my controller function as stated in the below and made no change in the form,
public function update(Request $request, User $setting)
{
$changedAttributes = array_diff($request->all(), $setting->getAttributes());
$validationRules = array_intersect_key([
'name' => ['required', 'alpha','min:2', 'max:255'],
'last_name' => ['required', 'alpha','min:5', 'max:255'],
'mobile' => ['required', 'string','min:10','max:14', 'regex:/\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|
2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|
4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.$setting->id.''],
'propic' => ['required','image','mimes:jpeg,png,jpg,gif,svg','max:2048'],
], $changedAttributes);
if($request->hasFile('propic'))
{
$this->validate($request, [
'name' => ['required', 'alpha','min:2', 'max:255'],
'last_name' => ['required', 'alpha','min:5', 'max:255'],
'mobile' => ['required', 'string','min:10','max:14', 'regex:/\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|
2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|
4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.$setting->id.''],
'propic' => ['required','image','mimes:jpeg,png,jpg,gif,svg','max:2048'],
],$request->all());
$imageName = time().'.'.$request->propic->extension();
$request->propic->move(public_path('propics'), $imageName);
$setting->propic=$imageName;
$setting->name=$request->input('name');
$setting->last_name=$request->input('last_name');
$setting->mobile=$request->input('mobile');
$setting->email=$request->input('email');
$setting->update();
return Redirect::back()->with('success',__('User updated successfully'));
}
$this->validate($request, $validationRules);
$setting->update($changedAttributes);
return Redirect::back()->with('success',__('User updated successfully'));
}
So this has worked for me.
Related
I am making a blogging application with Laravel 8 and Bootstrap 5.
I run into a problem with validating the image field of the "Add new article" form.
In the controller, I have:
namespace App\Http\Controllers\Dashboard;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use App\Models\ArticleCategory;
use App\Models\Article;
use Illuminate\Http\Request;
class ArticleController extends Controller
{
private $rules = [
'category_id' => ['required', 'exists:article_categories,id'],
'title' => ['required', 'string', 'max:255'],
'short_description' => ['required', 'string', 'max:255'],
'image' => ['mimes: jpeg, jpg, png, gif', 'max:2048'],
'content' => ['required', 'string']
];
private $messages = [
'category_id.required' => 'Please pick a category for the article',
'title.required' => 'Please provide a title for the article',
'short_description.required' => 'The article needs a short description',
'content.required' => 'Please add content'
];
public function categories() {
return ArticleCategory::all();
}
public function index() {
$articles = Article::paginate(10);
return view('dashboard/articles',
['articles' => $articles]
);
}
public function create() {
// Load the view and populate the form with categories
return view('dashboard/add-article',
['categories' => $this->categories()]
);
}
public function save(Request $request) {
// Validate form (with custom messages)
$validator = Validator::make($request->all(), $this->rules, $this->messages);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator->errors())->withInput();
}
$fields = $validator->validated();
// Upload article image
$current_user = Auth::user();
if (isset($request->image)) {
$imageName = md5(time()) . $current_user->id . '.' . $request->image->extension();
$request->image->move(public_path('images/articles'), $imageName);
}
// Data to be added
$form_data = [
'user_id' => Auth::user()->id,
'category_id' => $fields['category_id'],
'title' => $fields['title'],
'slug' => Str::slug($fields['title'], '-'),
'short_description' => $fields['short_description'],
'content' => $fields['content'],
'image' => $fields['image'],
'featured' => $fields['featured']
];
// Insert data in the 'articles' table
$query = Article::create($form_data);
if ($query) {
return redirect()->route('dashboard.articles')->with('success', 'Article added');
} else {
return redirect()->back()->with('error', 'Adding article failed');
}
}
}
The form:
<form method="POST" action="{{ route('dashboard.articles.add') }}">
#csrf
<div class="row mb-2">
<label for="title" class="col-md-12">{{ __('Title') }}</label>
<div class="col-md-12 #error('title') has-error #enderror">
<input id="title" type="text" placeholder="Title" class="form-control #error('title') is-invalid #enderror" name="title" value="{{ old('title') }}" autocomplete="title" autofocus>
#error('title')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-2">
<label for="short_description" class="col-md-12">{{ __('Short description') }}</label>
<div class="col-md-12 #error('short_description') has-error #enderror">
<input id="short_description" type="text" placeholder="Short description" class="form-control #error('short_description') is-invalid #enderror" name="short_description" value="{{ old('short_description') }}" autocomplete="short_description" autofocus>
#error('short_description')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-2">
<label for="category" class="col-md-12">{{ __('Category') }}</label>
<div class="col-md-12 #error('category_id') has-error #enderror">
<select name="category_id" id="category" class="form-control #error('category_id') is-invalid #enderror">
<option value="0">Pick a category</option>
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
#error('category_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-2">
<div class="col-md-12 d-flex align-items-center switch-toggle">
<p class="mb-0 me-3">Featured article?</p>
<input class="mt-1" type="checkbox" name="featured" id="featured">
<label class="px-1" for="featured">{{ __('Toggle') }}</label>
</div>
</div>
<div class="row mb-2">
<label for="image" class="col-md-12">{{ __('Article image') }}</label>
<div class="col-md-12 #error('image') has-error #enderror">
<input type="file" name="image" id="file" class="file-upload-btn">
#error('image')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-2">
<label for="content" class="col-md-12">{{ __('Content') }}</label>
<div class="col-md-12 #error('content') has-error #enderror">
<textarea name="content" id="content" class="form-control #error('content') is-invalid #enderror" placeholder="Content" cols="30" rows="6">{{ old('content') }}</textarea>
#error('content')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-0">
<div class="col-md-12">
<button type="submit" class="w-100 btn btn-primary">
{{ __('Save') }}
</button>
</div>
</div>
</form>
The routes:
// Article routes
Route::group(['prefix' => 'articles'], function() {
Route::get('/', [ArticleController::class, 'index'])->name('dashboard.articles');
Route::get('/new', [ArticleController::class, 'create'])->name('dashboard.articles.new');
Route::post('/add', [ArticleController::class, 'save'])->name('dashboard.articles.add');
Route::get('/delete/{id}', [ArticleController::class, 'delete'])->name('dashboard.articles.delete');
});
The problem:
Even if I upload a valid image (a PNG file) I get this validation error:
The image must be a file of type: jpeg, jpg, png, gif.
What is my mistake?
First of all, when you are dealing with an image/file you must have enctype=" multipart/form-data " in your form. Otherwise, you cannot submit the form. and validation will not be working.
Next validation rule for image or file will be like this :
'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048'
I am making a blogging application with Laravel 8 and Bootstrap 5.
I run into a problem trying to validate my "Add new article" form: when the form fails validation, the valid fields do not keep their values, despite ne using Blade's old() method.
In the controller, I have:
namespace App\Http\Controllers\Dashboard;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use App\Models\ArticleCategory;
use App\Models\Article;
use Illuminate\Http\Request;
class ArticleController extends Controller
{
private $rules = [
'category_id' => ['required', 'exists:article_categories,id'],
'title' => ['required', 'string', 'max:255'],
'short_description' => ['required', 'string', 'max:255'],
'image' => ['mimes: jpeg, jpg, png, gif', 'max:2048'],
'content' => ['required', 'string']
];
private $messages = [
'category_id.required' => 'Please pick a category for the article',
'title.required' => 'Please provide a title for the article',
'short_description.required' => 'The article needs a short description',
'content.required' => 'Please add content'
];
public function categories() {
return ArticleCategory::all();
}
public function index() {
$articles = Article::paginate(10);
return view('dashboard/articles',
['articles' => $articles]
);
}
public function create() {
// Load the view and populate the form with categories
return view('dashboard/add-article',
['categories' => $this->categories()]
);
}
public function save(Request $request) {
// Validate form (with custom messages)
$validator = Validator::make($request->all(), $this->rules, $this->messages);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator->errors());
}
$fields = $validator->validated();
// Upload article image
$current_user = Auth::user();
if (isset($request->image)) {
$imageName = md5(time()) . $current_user->id . '.' . $request->image->extension();
$request->image->move(public_path('images/articles'), $imageName);
}
// Data to be added
$form_data = [
'user_id' => Auth::user()->id,
'category_id' => $fields['category_id'],
'title' => $fields['title'],
'slug' => Str::slug($fields['title'], '-'),
'short_description' => $fields['short_description'],
'content' => $fields['content'],
'image' => $fields['image'],
'featured' => $fields['featured']
];
// Insert data in the 'articles' table
$query = Article::create($form_data);
if ($query) {
return redirect()->route('dashboard.articles')->with('success', 'Article added');
} else {
return redirect()->back()->with('error', 'Adding article failed');
}
}
}
The form:
<form method="POST" action="{{ route('dashboard.articles.add') }}">
#csrf
<div class="row mb-2">
<label for="title" class="col-md-12">{{ __('Title') }}</label>
<div class="col-md-12 #error('title') has-error #enderror">
<input id="title" type="text" placeholder="Title" class="form-control #error('title') is-invalid #enderror" name="title" value="{{ old('title') }}" autocomplete="title" autofocus>
#error('title')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-2">
<label for="short_description" class="col-md-12">{{ __('Short description') }}</label>
<div class="col-md-12 #error('short_description') has-error #enderror">
<input id="short_description" type="text" placeholder="Short description" class="form-control #error('short_description') is-invalid #enderror" name="short_description" value="{{ old('short_description') }}" autocomplete="short_description" autofocus>
#error('short_description')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-2">
<label for="category" class="col-md-12">{{ __('Category') }}</label>
<div class="col-md-12 #error('category_id') has-error #enderror">
<select name="category_id" id="category" class="form-control #error('category_id') is-invalid #enderror">
<option value="0">Pick a category</option>
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
#error('category_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-2">
<div class="col-md-12 d-flex align-items-center switch-toggle">
<p class="mb-0 me-3">Featured article?</p>
<input class="mt-1" type="checkbox" name="featured" id="featured">
<label class="px-1" for="featured">{{ __('Toggle') }}</label>
</div>
</div>
<div class="row mb-2">
<label for="image" class="col-md-12">{{ __('Article image') }}</label>
<div class="col-md-12 #error('image') has-error #enderror">
<input type="file" name="image" id="file" class="file-upload-btn">
#error('image')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-2">
<label for="content" class="col-md-12">{{ __('Content') }}</label>
<div class="col-md-12 #error('content') has-error #enderror">
<textarea name="content" id="content" class="form-control #error('content') is-invalid #enderror" placeholder="Content" cols="30" rows="6">{{ old('content') }}</textarea>
#error('content')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row mb-0">
<div class="col-md-12">
<button type="submit" class="w-100 btn btn-primary">
{{ __('Save') }}
</button>
</div>
</div>
</form>
The routes:
// Article routes
Route::group(['prefix' => 'articles'], function() {
Route::get('/', [ArticleController::class, 'index'])->name('dashboard.articles');
Route::get('/new', [ArticleController::class, 'create'])->name('dashboard.articles.new');
Route::post('/add', [ArticleController::class, 'save'])->name('dashboard.articles.add');
Route::get('/delete/{id}', [ArticleController::class, 'delete'])->name('dashboard.articles.delete');
});
The problem:
I was expecting this syntax for keeping the values of valid fields in an invalid form, should work: value="{{ old('title') }}". But, fo a reason I have been enable to spot, it does not. The form is reset entirely.
What is my mistake?
You're redirecting back to the previous page without telling Laravel to pass the old inputs. Use the method withInput() with the route return.
Change
return redirect()->back()->withErrors($validator->errors());
with
return redirect()->back()->withErrors($validator->errors())->withInput();
You can use this method instead, and then the return with inputs will happen automatically
$fields = $this->validate($request->all(), $this->rules, $this->messages);
// if ($validator->fails()) {
// return redirect()->back()->withErrors($validator->errors());
// }
By default the registration page has name, email, password and confirm password on its form like the following:
<div class="container d-flex flex-column justify-content-between vh-100">
<div class="row justify-content-center mt-5">
<div class="col-xl-5 col-lg-6 col-md-10">
<div class="card">
<div class="card-header bg-primary">
<div class="app-brand">
<a href="/index.html">
</a>
</div>
</div>
<div class="card-body p-5">
<h4 class="text-dark mb-5">Register</h4>
<form method="POST" action="{{ route('register') }}">
#csrf
<div class="row">
<div class="form-group col-md-12 mb-4">
<input type="text" name="name" :value="old('name')" required autofocus autocomplete="name" class="form-control input-lg" id="name" aria-describedby="nameHelp" placeholder="Name">
#if ($errors->has('name'))
<span class="invalid-feedback">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
<div class="form-group col-md-12 mb-4">
<input type="email" name="email" :value="old('email')" required class="form-control input-lg" id="email" aria-describedby="emailHelp" placeholder="Email">
</div>
<div class="form-group col-md-12 ">
<input type="password" name="password" required autocomplete="new-password" class="form-control input-lg" id="password" placeholder="Password">
</div>
<div class="form-group col-md-12 ">
<input type="password" name="password_confirmation" required autocomplete="new-password" class="form-control input-lg" id="cpassword" placeholder="Confirm Password">
</div>
<div class="col-md-12">
<input type="checkbox" name="terms" value="true" {{ !old('terms') ?: 'checked' }}>
<label>Do you agree to Terms & Conditions</label><br/><br/>
<button type="submit" class="btn btn-lg btn-primary btn-block mb-4">Sign Up</button>
<p>Already have an account?
<a class="text-blue" href="{{ route('login') }}">Sign in</a>
</p>
</div>
</div>
</form>
</div>
</div>
</div>
Then on the User Model we have the following fillable data:
protected $fillable = [
'name',
'email',
'password',
];
Since I am a beginner with laravel, I want to add two fields and make sure that they are also saved in the database. I want to add the phone and date of birth fields.
I was wondering what are the steps to make in order to make this fillable and add them to the database?
You have two options to do it
1.Adding columns in fillable like below
protected $fillable = [
'name',
'email',
'password',
'username',
'phone',
'date_of_birth'
];
2.using $guarded
protected $guarded=['id'];
this will allow fillable all other than id
Add two inputs in blade file
<div class="form-group col-md-12 mb-4">
<input type="text" name="phone" :value="old('phone')" required autofocus autocomplete="phone" class="form-control input-lg" id="name" aria-describedby="nameHelp" placeholder="Phone">
#if ($errors->has('phone'))
<span class="invalid-feedback">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
<div class="form-group col-md-12 mb-4">
<input type="text" name="date_of_birth" :value="old('date_of_birth')" required autofocus autocomplete="date_of_birth" class="form-control input-lg" id="name" aria-describedby="nameHelp" placeholder="Date Of Birth">
#if ($errors->has('date_of_birth'))
<span class="invalid-feedback">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
In App\Http\Controllers\Auth\RegisterController change method
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'phone'=>$data['phone'],
'date_of_birth'=>$data['date_of_birth'],
]);
}
If you need validation then you add that too
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'phone' => ['required', 'number'],
'date_of_birth' => ['required',],
]);
}
I am trying to insert data of a user in the database using ajax. I have done it without it and it worked fine. Now it is giving me error 422. The code is given below:
UserController.php
public function store(Request $request)
{
$request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
$inputArray = array(
'name' => $request['name'],
'email' => $request['email'],
'password' => Hash::make($request['password']),
);
$value = User::create($inputArray);
!is_null($value) ? $message = "User Added Successfully" : $message = "User not Added";
return json_encode(array($message));
}
Index.blade.php
<form method="post" {{-- action="{{ route('users.store') }}" --}} id="createform">
<input type="hidden" name="_token" id="csrf" value="{{Session::token()}}">
<div class="input-group mb-3">
<input type="text"
name="name"
class="form-control #error('name') is-invalid #enderror"
value="{{ old('name') }}"
placeholder="Full name"
id="name">
<div class="input-group-append">
<div class="input-group-text">#icon('user')</div>
</div>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="input-group mb-3">
<input type="email"
name="email"
value="{{ old('email') }}"
class="form-control #error('email') is-invalid #enderror"
placeholder="Email"
id="email">
<div class="input-group-append">
<div class="input-group-text">#icon('envelope')</div>
</div>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="input-group mb-3">
<input type="password"
name="password"
class="form-control #error('password') is-invalid #enderror"
placeholder="Password"
id="password">
<div class="input-group-append">
<div class="input-group-text">#icon('lock')</div>
</div>
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="input-group mb-3">
<input type="password"
name="password_confirmation"
class="form-control"
placeholder="Retype password"
id="password_confirmation">
<div class="input-group-append">
<div class="input-group-text">#icon('lock')</div>
</div>
</div>
<div id="msg"></div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary" id="btnadd">Add User</button>
</div>
</form>
jqajax.js
jQuery(function ($) {
jQuery("#btnadd").on('click', addInfo);
function addInfo(e) {
e.preventDefault();
var name = jQuery('#name').val();
var email = $("#email").val();
var password = $("#password").val();
var password_confirmation = $("#password_confirmation").val();
$.ajax({
url: "/users",
method: "POST",
data: {
_token: $("#csrf").val(),
name: name,
email: email,
password: password,
password_confirmation: password_confirmation
},
success: function (dataResult) {
var dataResult = JSON.parse(dataResult);
msg = "<div class='alert alert-dark mt-3'>dataResult.message</div>";
$("#msg").html(msg);
$("#createform")[0].reset();
showdata();
},
});
}
});
The Error I am getting is app.js:14653 POST http://127.0.0.1:8000/users 422 (Unprocessable Entity)
function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => ['required', 'string', 'max:255']
]);
if ($validator->fails()) {
$errors = $validator->errors();
$returnError['name'] = $errors->first('name');
return json_encode($returnError);
} else {
$inputArray = array(
'name' => $request['name']
);
$value = User::create($inputArray);
!is_null($value) ? $message = "User Added Successfully" : $message = "User not Added";
return json_encode(array($message));
}
}
I have a user update form with a user photo. Currently I'm saving my user photo (file name) in my user table's propic column.
The Problem
I'm trying to add a remove photo button when a user wants to remove the uploaded image. Once the user click the remove photo button user's propic column value need to be reset to default value (user-photo.png). And I need to display that remove photo button only for the users who have updated their profile photos. Users with default profile photo doesn't need the option.
What I have done so far
This is my form, in user blade
<form action="{{ route('settings.update',$user->id) }}" method="POST" enctype="multipart/form-data">
<div class="row mt-5">
<div class="col-sm-3">
<img src="/propics/{{$user->propic}}" alt="Profile Pic" id="profile_pic_display" class="mb-3">
<input type="file" name="propic" class="form-control">
#error('propic')
<span class="help-block" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="col-sm-9">
<!-- <form action="{{ route('settings.update',$user->id) }}" method="POST"> -->
#csrf
#method('PUT')
<div class="row">
<div class="col-md-6">
<div class="form-group field-user-firstname required">
<label class="control-label"
for="user-firstname">{{ __('sentence.First Name') }}</label>
<input id="name" type="text" class="form-control #error('name') is-invalid #enderror"
name="name" value="{{$user->name}}" autocomplete="name" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-md-6">
<div class="form-group field-user-lastname required">
<label class="control-label" for="user-lastname">{{ __('sentence.Last Name') }}</label>
<input id="last_name" type="text"
class="form-control #error('name') is-invalid #enderror" name="last_name"
value="{{$user->last_name}}" autocomplete="last_name" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group field-user-mobile required">
<label class="control-label" for="user-mobile">{{ __('sentence.Mobile') }}</label><br/>
<input id="mobile_1" type="tel"
class="form-control #error('mobile') is-invalid #enderror" name="mobile"
value="{{$user->mobile}}" style="min-width:398px;" autocomplete="mobile"
autofocus>
#error('mobile')
<span class="help-block" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-md-6">
<div class="form-group field-user-email required">
<label class="control-label" for="user-email">{{ __('sentence.Email') }}</label>
<input id="email_" type="email"
class="form-control #error('email') is-invalid #enderror" name="email"
value="{{$user->email}}" autocomplete="email">
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
</div>
<div class="row">
<div class="col-sm">
<div class="form-group pull-right">
<button type="submit"
class="btn btn-default">{{ __('sentence.Cancel') }}</button>
<button type="submit"
class="btn btn-default subscribe px-5">{{ __('sentence.Update') }}</button>
</div>
</div>
</div>
</div>
</form>
And following is my controller (Only the update function is included)
public function update(Request $request, User $setting)
{
$changedAttributes = array_diff($request->all(), $setting->getAttributes());
$validationRules = array_intersect_key([
'name' => ['required', 'alpha','min:2', 'max:255'],
'last_name' => ['required', 'alpha','min:5', 'max:255'],
'mobile' => ['required', 'numeric','min:9','regex:/\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|
2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|
4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.$setting->id.''],
'propic' => ['required','image','mimes:jpeg,png,jpg,gif,svg','max:2048'],
], $changedAttributes);
if($request->hasFile('propic'))
{
$this->validate($request, [
'name' => ['required', 'alpha','min:2', 'max:255'],
'last_name' => ['required', 'alpha','min:5', 'max:255'],
'mobile' => ['required', 'numeric','min:9','regex:/\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|3[70]|7|1)\d{1,14}$/'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.$setting->id.''],
'propic' => ['required','image','mimes:jpeg,png,jpg,gif,svg','max:2048'],
],$request->all());
$imageName = time().'.'.$request->propic->extension();
$request->propic->move(public_path('propics'), $imageName);
$setting->propic=$imageName;
$setting->name=$request->input('name');
$setting->last_name=$request->input('last_name');
$setting->mobile=$request->input('mobile');
$setting->email=$request->input('email');
$setting->update();
return Redirect::back()->with('success',__('sentence.User updated successfully'));
}
$this->validate($request, $validationRules);
$setting->update($changedAttributes);
return Redirect::back()->with('success',__('sentence.User updated successfully'));
}
}
First you need to check in your view if user profile photo is not "user-photo.png" and if true insert a button to reset photo:
#if($user->propic != 'user-photo.png')
<button type="submit" name="resetphoto" class="btn btn-warning">Reset Profile Pic</button>
#endif
Then in your controller at the beginning of update() method check if the user has clicked on "Reset Profile Pic" button, and then if is true set the column back to default value, then save the model
if ($request->has('resetphoto')){
$setting->propic = 'user-photo.png';
$setting->update();
return Redirect::back()->with('success',__('sentence.User profile pic reset successfully'));
}
PS: Im assuming your "$setting" is the User model