In this comprehensive guide on Laravel Custom Authentication, you will learn how to manage profile updates efficiently. The article is divided into three essential sections: Basic Profile Update, where you'll follow a step-by-step process to update user details such as name and email; Profile Photo Update, which covers implementing functionality for users to upload and change their profile photos; and Password Update, focusing on best practices for allowing users to update their passwords securely. Following this guide will enhance your Laravel application with robust and user-friendly profile management features, ensuring a seamless experience for your users.
The first article link is in the Laravel Custom Authentication.
I have already told you I have divided this form into three parts. Let's first, see the form code.
Basic profile form Code
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-body">
<div class="icon-contain">
{{-- Basic Details --}}
<div class="profile-content-container">
<div class="profile-content-header">
<div class="profile-content">
<h6>Basic Profile</h6>
<a href="{{route('customer.dashboard')}}" class="backbuttonlink">Back</a>
</div>
</div>
</div>
<div class="profile-content-container mt-4">
<div class="profile-content-body">
<div class="profile-content">
<form action="{{ route('customer.profiles.submit', ['id' => $customerRecord->id]) }}" method="post" enctype="multipart/form-data">
@if (Session::has('success'))
<div class="alert alert-success">
{{ Session::get('success') }}
</div>
@endif
@if (Session::has('error'))
<div class="alert alert-danger">
{{ Session::get('error') }}
</div>
@endif
@csrf
<div class="row">
<div class="col-lg-6">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" id="name" value="{{$customerRecord->name}}">
</div>
<div class="col-lg-6">
<label for="name">Email</label>
<input type="email" class="form-control" name="email" id="email" value="{{$customerRecord->email}}">
</div>
</div>
<div class="row mt-4">
<div class="col-lg-6">
<label for="name">Contact</label>
<input type="text" class="form-control" name="contact" id="contact" value="{{$customerRecord->contact}}">
</div>
<div class="col-lg-6">
<label for="name">Date of Birth</label>
<input type="text" class="form-control" name="dob" id="mdate" value="{{$customerRecord->dob}}" data-dtp="dtp_ngM38">
</div>
</div>
<div class="row mt-4">
<div class="col-lg-12">
<button type="submit" class="submitBtn">SUBMIT</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
GET SOURCE CODE
Profile photo from code
<div class="container-fluid">
<div class="row">
<div class="col-lg-6">
<div class="card">
<div class="card-body">
<h4 class="mt-0 header-title">File Upload</h4>
<form action="{{ route('customer.photo.submit', ['id' => $customerRecord->id]) }}" method="post" enctype="multipart/form-data">
@csrf
<div class="dropify-wrapper">
<div class="dropify-message">
<span class="file-icon"></span>
<p>Drag and drop a file here or click</p>
<p class="dropify-error">Ooops, something wrong appended.</p>
</div>
<div class="dropify-loader"></div>
<div class="dropify-errors-container">
<ul></ul>
</div>
<input type="file" id="input-file-now" class="dropify" name="profileimage">
<button type="button" class="dropify-clear">Remove</button>
<div class="dropify-preview">
<span class="dropify-render"></span>
<div class="dropify-infos">
<div class="dropify-infos-inner">
<p class="dropify-filename">
<span class="file-icon"></span>
<span class="dropify-filename-inner"></span>
</p>
<p class="dropify-infos-message">Drag and drop or click to replace</p>
</div>
</div>
</div>
</div>
<div class="row mt-4">
<div class="col-lg-12">
<button type="submit" class="submitBtn">SAVE</button>
</div>
</div>
</form>
</div>
</div>
</div>
<div class="col-lg-6">
<div class="card" style="height: 336px">
<div class="card-body d-flex justify-content-center align-items-center">
<img src="{{ asset('Customer/profile/' . $customerRecord->profile) }}" alt="user" class="rounded-circle img-thumbnail mb-1">
</div>
</div>
</div>
</div>
</div>
Password from code
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-body">
<div class="icon-contain">
<div class="profile-content-container">
<div class="profile-content-header">
<div class="profile-content">
<h6>Password Update</h6>
</div>
</div>
</div>
<div class="profile-content-container mt-4">
<div class="profile-content-body">
<div class="profile-content">
<form action="{{ route('customer.password.update', ['id' => $customerRecord->id]) }}" method="post" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="col-lg-12">
<label for="currentpassword">Current Password</label>
<input type="password" class="form-control @error('currentpassword') is-invalid @enderror" name="currentpassword" id="currentpassword">
@error('currentpassword')
<span class="invalid-feedback">{{ $message }}</span>
@enderror
</div>
</div>
<div class="row mt-4">
<div class="col-lg-6">
<label for="newpassword">New Password</label>
<input type="password" class="form-control @error('newpassword') is-invalid @enderror" name="newpassword" id="newpassword">
@error('newpassword')
<span class="invalid-feedback">{{ $message }}</span>
@enderror
</div>
<div class="col-lg-6">
<label for="confirmpassword">Confirm password</label>
<input type="password" class="form-control @error('confirmpassword') is-invalid @enderror" name="confirmpassword" id="confirmpassword">
@error('confirmpassword')
<span class="invalid-feedback">{{ $message }}</span>
@enderror
</div>
</div>
<div class="row mt-4">
<div class="col-lg-12">
<button type="submit" class="submitBtn">SUBMIT</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Now look at the route code I have used the same route file that I used earlier in the article "Laravel Custom Authentication".
\customerauth\routes\Customer\Dashboard.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Customer\Auth\CustomerController;
use App\Http\Controllers\Customer\DashboardController;
Route::prefix('customer')->middleware('iscustomerAuth')->group(function () {
route::get('/dashboard', [DashboardController::class, 'dashboard'])->name('customer.dashboard');
route::get('/profiles',[DashboardController::class , 'CustomerProfiles'])->name('customer.profiles');
// Profile Update route
Route::post('/profiles/submit/{id}', [DashboardController::class, 'CustomerProfilesSubmit'])->name('customer.profiles.submit');
Route::post('/profiles/photo/{id}', [DashboardController::class, 'CustomerPhotoSubmit'])->name('customer.photo.submit');
Route::post('/password/update/{id}', [DashboardController::class, 'CustomerPasswordUpdate'])->name('customer.password.update');
});
Controller code
\customerauth\app\Http\Controllers\Customer\DashboardController.php
<?php
namespace App\Http\Controllers\Customer;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
use Hash;
use Session;
use App\Models\Customer;
use App\Models\PasswordUpdateHistory;
use Carbon\Carbon;
class DashboardController extends Controller
{
public function dashboard()
{
if(Auth::guard('customer')->check())
{
return view('Customer.Dashboard');
}
else
{
return redirect()->route('customer.login');
}
}
public function CustomerProfiles()
{
$email = Auth::guard('customer')->user()->email;
$customerRecord = Customer::where('email', $email)->first();
return view('Customer.Auth.profile', compact('customerRecord'));
}
public function CustomerProfilesSubmit(request $request, $id)
{
$customerRecord = Customer::find($id);
if($customerRecord)
{
$customerRecord->name = $request->input('name');
$customerRecord->email = $request->input('email');
$customerRecord->dob = $request->input('dob');
$customerRecord->contact = $request->input('contact');
$customerRecord->update();
if($customerRecord)
{
return redirect()->back()->with('success', 'Record successfully update!');
}
else
{
return redirect()->back()->with('error', 'Record not update!');
}
}
else
{
return redirect()->back()->with('error', 'Record not found!');
}
}
public function CustomerPhotoSubmit(Request $request, $id)
{
$request->validate([
'profileimage' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$customerRecord = Customer::find($id);
if ($customerRecord) {
if ($request->hasFile('profileimage')) {
$profile = $request->file('profileimage');
$profileRename = uniqid() . '.' . $profile->getClientOriginalName();
$profileLocation = public_path('/Customer/profile');
$customerRecord->profile = $profileRename;
$customerRecord->update();
if($customerRecord)
{
$profile->move($profileLocation, $profileRename);
return redirect()->back()->with('success', 'Profile photo update!');
}
else
{
return redirect()->back()->with('error', 'Profile photo not update!');
}
}
} else {
return redirect()->back()->with('error', 'Customer record not found');
}
}
public function CustomerPasswordUpdate(Request $request, $id)
{
$request->validate([
"currentpassword" => "required|min:6",
"newpassword" => "required|min:6",
"confirmpassword" => "required|min:6|same:newpassword",
],
[
"currentpassword.required" => "The current password field is required.",
"currentpassword.min" => "The current password must be at least 6 characters.",
"newpassword.required" => "The new password field is required.",
"newpassword.min" => "The new password must be at least 6 characters.",
"confirmpassword.required" => "The confirmation password field is required.",
"confirmpassword.min" => "The confirmation password must be at least 6 characters.",
"confirmpassword.same" => "The confirmation password must match the new password.",
]);
$customerRecord = Customer::find($id);
if($customerRecord)
{
if(Hash::check($request->input('currentpassword'), $customerRecord->password))
{
$lastPassword = $customerRecord->password;
$customerRecord->password = Hash::make($request->input('newpassword'));
$customerRecord->update();
if($customerRecord)
{
$history = new PasswordUpdateHistory();
$history->customer_id = $id;
$history->password_update_date = Carbon::now();
$history->last_password = $lastPassword;
$history->save();
if($history)
{
return redirect()->back()->with('success', 'Password successfully updated!');
}
else
{
return redirect()->back()->with('error', 'Password not updated!');
}
}
}
else
{
return redirect()->back()->with('error', 'Incorrect current password!');
}
}
else
{
return redirect()->back()->with('error', 'Record not found!');
}
}
}