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;
}
});
Related
So I am making a crud application. But i have a problem in my edit part. I setted up the edit view and the action to the update function, where I setted up the update too. But when I hit submit it wont redirect me to the page and it wont update, it will stay to the edit page, and its not giving me any error!
This is my form(don't mind the style):
<form method="post" action="{{route('profile.update', $user->id)}}" enctype="multipart/form-data">
#csrf
#method('PATCH')
<div class="col-8 offset-2">
<div class="row">
<h1>Edit Profile</h1>
</div>
<div class="form-group row">
<label for="title" class="col-md-4 col-form-label">Title</label>
<input id="title"
type="text"
class="form-control #error('name') is-invalid #enderror"
name="title"
value="{{$user->profile->title}}"
required autocomplete="title" autofocus>
#error('title')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Description</label>
<input id="description"
type="text"
class="form-control #error('name') is-invalid #enderror"
name="description"
value="{{$user->profile->description}}"
required autocomplete="description" autofocus>
#error('description')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group row">
<label for="url" class="col-md-4 col-form-label">Url</label>
<input id="url"
type="text"
class="form-control #error('name') is-invalid #enderror"
name="url"
value="{{$user->profile->url}}"
required autocomplete="url" autofocus>
#error('url')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="row">
<label for="image" class="col-md-4 col-form-label">Profile Image</label>
<input type="file" class="form-control-file" id="image" name="image">
#error('image')
<strong>{{ $message }}</strong>
#enderror
</div>
<div class="row pt-4">
<button type="submit" class="btn btn-primary w-25">Save Profile</button>
</div>
</div>
</form>
Here is my route:
Route::patch('/profile/{user}', [ProfilesController::class, 'update'])->name('profile.update');
Here is my controller:
public function update(User $user)
{
$data = request()->validate([
'title'=>'required',
'description'=>'required',
'url'=>'url',
'image'=>'file|mimes:jpeg,jpg,png,giv,svg'
]);
auth()->user()->profile()->update($data);
return redirect()->route('profile.show', $user->id);
}
So I ran the code to display the validation errors:
And I got this:
So I didn't have a valid URL and than I fixed it to this, now I can update:
I am making a blogging application with Laravel 8 and Bootstrap 5.
I run into a problem trying to validate my Add article form. It fails to validate, meaning that no error messages appear, even if the required form data is not filled.
In the ArticleController 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:categories,id',
'title' => 'required|string',
'short_description' => 'required|string',
'content' => 'required|longText'
];
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 add(Request $request) {
// Pass the categories to the articles form
return view('dashboard/add-article',
['categories' => $this->categories()]
);
// 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();
// 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()->route('dashboard.articles')->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" value="{{ old('image') }}" 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::match(['get', 'post'],'/add', [ArticleController::class, 'add'])->name('dashboard.articles.add');
Route::get('/delete/{id}', [ArticleController::class, 'delete'])->name('dashboard.articles.delete');
});
Why does my form fail to validate?
Your public function add() starts with a return statement.
The rest of the code is irrelevant since you return from the function at this point.
I am a newbie in Laravel and I need your help.
I'm using the Laravel 7. I have 3 tables in my database.
User to save the registered users
Categories to show in the front-end that I provided for them to choose (multiple)
Vendor_Cat that I collecting the user_id and category_id after the user registered
Here are my Models
public function users(){
return $this->BelongsToMany(User::class);
}
public function categories(){
return $this->BelongsToMany(Categories::class);
}
This one below is my VendorCategories Model
protected $fillable = [
'category_id',
'vendor_id'
];
And this is the Laravel RegisterController
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'tel' => $data['tel'],
'lineid'=> isset($data['lineid']) ? $data['lineid'] : null,
]);
My Front-End code (Just Categories for a user to choose)
#if($categories->count()>0)
<div class="form-row">
<label class="form-row-inner">
<select class="selectpicker col-sm-12 md-12" id="selectpicker" name="categories[]" multiple data-live-search="true" data-selected-text-format="count" data-count-selected-text="Selected Categories({0})" title="Choose your catories" aria-expanded="false" aria-haspopup="true" required>
#foreach ($categories as $categories)
<option class="com-sm-4 md-4" aria-expanded="false" value="{{$categories->id}}"
#if (isset($user))
#if($user->hasCat($catgories->id))
selected
#endif
#endif
><strong>{{Str::limit($categories->name,51)}}</strong></option>
#endforeach
</select>
#error('category_id')
<span class="invalid-feedback-detail" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
#endif
Full Register Form Code
<form class="form-detail col-xs-12 sm-12" action="{{ route('register') }}" method="post">
#csrf
<div class="tabcontent" id="sign-up">
<div class="form-row">
<label class="form-row-inner">
<input type="text" id="full_name" class="input-text #error('name') is-invalid #enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
<span class="label">Full Name</span>
#error('name')
<span class="invalid-feedback-detail" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<span class="border"></span>
</label>
</div>
<div class="form-row">
<label class="form-row-inner">
<input type="text" id="your_email" class="input-text #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
<span class="label">E-Mail</span>
#error('email')
<span class="invalid-feedback-detail" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<span class="border"></span>
</label>
</div>
<div class="form-row">
<label class="form-row-inner">
<input type="text" id="yournumber" class="input-text #error('tel') is-invalid #enderror" name="tel" required autocomplete="new-password">
<span class="label">Telephone Number</span>
#error('tel')
<span class="invalid-feedback-detail" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<span class="border"></span>
</label>
</div>
<div class="form-row">
<label class="form-row-inner">
<input type="password" name="password" id="password" class="input-text #error('password') is-invalid #enderror" name="password" required autocomplete="new-password">
<span class="label">Password</span>
#error('password')
<span class="invalid-feedback-detail" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<span class="border"></span>
</label>
</div>
<div class="form-row">
<label class="form-row-inner">
<input type="password" id="comfirm_password" class="input-text" name="password_confirmation" required autocomplete="new-password">
<span class="label">Confirm Password</span>
<span class="border"></span>
</label>
</div>
<div class="form-row">
<label class="form-row-inner">
<input type="text" id="comfirm_password_1" class="input-text #error('lineid') is-invalid #enderror" name="lineid" required autocomplete="new-password">
<span class="label">Line Id</span>
#error('lineid')
<span class="invalid-feedback-detail" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<span class="border"></span>
</label>
</div>
#if($categories->count()>0)
<div class="form-row">
<label class="form-row-inner">
<select class="selectpicker col-sm-12 md-12" id="selectpicker" name="categories[]" multiple data-live-search="true" data-selected-text-format="count" data-count-selected-text="หมวดหมู่ที่เลือก ({0} หมวดหมู่)" title="เลือกหมวดหมู่ที่ให้บริการ" aria-expanded="false" aria-haspopup="true" required>
#foreach ($categories as $categories)
<option class="com-sm-4 md-4" aria-expanded="false" value="{{$categories->id}}"
#if (isset($user))
#if($user->hasCat($catgories->id))
selected
#endif
#endif
><strong>{{Str::limit($categories->name,51)}}</strong></option>
#endforeach
</select>
#error('category_id')
<span class="invalid-feedback-detail" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
#endif
<div class="form-row-last">
<input type="submit" name="register" class="register">
</div>
</div>
</form>
If I want to save the category Ids that I use #foreach to show in the Register Form after the users selected. What should I do?
This is the thing that I have tried before
protected function create(array $data){
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'tel' => $data['tel'],
'lineid'=> isset($data['lineid']) ? $data['lineid'] : null,
]);
$vendor_cat = VendorCat::create([
'category_id' => $data['category'],
'vendor_id' => $user->id
]);
return $user;
}
Here is the explanation image
Click Here
Thank you,
p.s. sorry for my bad English.
Add registered method in your RegisterController, to override the register method from your RegistersUsers trait. Also add use Illuminate\Http\Request; in your RegisterController.
protected function create(array $data)
{
// .... your create method in RegisterController
}
protected function registered( Request $request, $user )
{
VendorCat::create([
'category_id' => $request['category'],
'vendor_id' => $user->id
]);
}
EDIT:
as you are getting categories as array, you can simply store all the keys separated by commas, or store them separately for each values,
To store them separately,
foreach($request['categories'] as $category){
VendorCat::create([
'category_id' => $category,
'vendor_id' => $user->id
]);
}
It will create 3 columns for 3 different category_id with same vendor_id.
To store using commas,
VendorCat::create([
'category_id' => is_array($request['categories']) ? implode(",",$request['categories']) : $request['categories'],
'vendor_id' => $user->id
]);
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
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