Remove uploaded profile picture and reset to default image in laravel issue - php

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

Related

What makes the image type validation in this Laravel application fail?

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'

Why does the old() method fail in this Laravel 8 form?

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());
// }

How to add fields on the default Jetstream Registration Page in Laravel 8

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',],
]);
}

Sending email activation(confirmation) link when user UPDATING email address in Laravel

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;
}
});

Laravel Image Uploading Issue

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.

Categories