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));
}
}
Related
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());
// }
I have login form in Laravel that uses email and password to log on site. I have all validation and everything works fine except for password. When I type wrong password it goes to blank page and I want to write some error beneath password field. I looked in same:password validation but it doesn't work. Any help is appreciated. Here is my code.
LoginController.php
public function login(Request $request)
{
$rules = [
'email' => 'required|email|exists:App\User,email',
'password' => 'required|alphaNum|min:5'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
$request->session()->put('data', $request->input());
return redirect()->route('login')
->withErrors($validator->errors())
->withInput($request->session()->put('data', $request->input()));
} else {
$userData = array(
'email' => $request->get('email'),
'password' => $request->get('password')
);
}
if (Auth::attempt($userData)) {
return redirect()->route('dashboard');
} else {
redirect()->route('login');
}
}
login.blade.php
<div class="login-page">
<div class="login-box">
<div class="card mb-0">
<div class="card-body login-card-body">
<p class="login-box-msg font-weight-bold">Sign in to start your session</p>
<form method="POST" class="mb-4" action="{{route('login') }}">
#csrf
<div class="input-group mb-3">
<input id="email" type="email" placeholder="Email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
<div class="input-group-append">
<div class="input-group-text">
<span class="fas fa-user"></span>
</div>
</div>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="input-group mb-3">
<input id="password" type="password" placeholder="Password" class="form-control #error('password') is-invalid #enderror" name="password" required autocomplete="current-password">
<div class="input-group-append">
<div class="input-group-text">
<span class="fas fa-eye-slash cursor-pointer" style="display: none" onclick="showPassword()"></span>
<span class="fas fa-eye cursor-pointer" onclick="showPassword()"></span>
</div>
</div>
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="float-right">
<button type="submit" class="btn btn-primary btn-block font-weight-bold">Sign In</button>
</div>
</form>
</div>
</div>
</div>
</div>
Assuming that your email is unique, you first have to get the DB row where the $request->email is: $user = User::where('email', $request->email)->first()
You can then check it by using: Hash::check($request->password, $user->password)
https://laravel.com/docs/7.x/hashing
#edit
To add it to the rules you will have to create a Rule Class: php artisan make:rule myRuleName
Afterwards you will call it like that:
$rules = [
'email' => 'required|email|exists:App\User,email',
'password' => ['required','alphaNum','min:5', new myRuleName()],
];
In your custom Rule Class you will find a passes($attribute, $value)function.
Now you can insert the code i wrote above into this method. You will have to replace $request->password with $value
How can I resend my activation account email to my user once the user update the email address.
Email address should not be changed in the db until user click on the confirmation link
following is my update function for an user in my usercontroller(only the function body included)
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'));
}
and following is my update user form
<form action="{{ route('settings.update',$user->id) }}" method="POST" enctype="multipart/form-data">
<div class="row mt-5">
<div class="col-sm-3">
#if($user->propic != 'user-pic.png')
<button type="submit" name="resetphoto" class="btn btn-warning pull-right">{{ __('sentence.Remove Profile Pic') }}</button>
#endif
<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>
Currently i'm sending activation link to user email when the user is registering, but I'm struggling to do it when an existing user is trying to update email address..
Your question isn't entirely clear because you're working in a box here and haven't given us the full picture. If the intention is to ensure that the email cannot unless they've activated, then re-send them the activation email and confirm the new address upon activation, then you're missing some logic here.
Any way that you look at it, you need to store a reference to what the email should be somewhere. Either a column, such as pending_email on the table, or it's own table, such as pending_changes for instance.
In your boot method of your User model you can watch for the updating event and dispatch a job from there to re-send the activation email and halt the change of the email address:
public static function boot()
{
parent::boot();
static::updating(function ($model) {
if ($model->isDirty('email') && $model->activated_at === null) {
$model->email = $model->getOriginal('email');
dispatch (new SendActivationEmail($model));
}
});
}
Now you can append to that updating event to see if they're activating and have a pending_email:
static::updating(function ($model) {
if ($model->isDirty('activated_at') && $model->pending_email) {
$model->email = $model->pending_email;
$model->pending_email = null;
}
});
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
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.