Category : #laravel
Tags : #laravel, #laravel auth
In this tutorial, I will show you laravel update user profile tutorial. We are going to create laravel 9 update user profile, so we need authentication. So we will create default auth in laravel 9. Cause in this example, our main focus is laravel update user profile. So we will create default auth for quickness.
If you know how to fetch authenticated user data in laravel, then it is very easy to create how to update user profile in laravel 9. Using Auth
facades or auth()
helper, we can get authenticated user data in larave.
Step 1: Install Laravel
First of all, we need to get a fresh Laravel 9 version application using the bellow command, So open your terminal OR command prompt and run the bellow command to start laravel 9 update user tutorial.
composer create-project laravel/laravel example-app
Read also: Laravel 9 Multi Auth - Create Multiple Authentication In Laravel
Step 2: Connect Database
We need registration, so need database table to save user data. We will open the ".env" file and change the database name, username and password in the env file and create laravel update user profile.
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_Your_Database_Name
DB_USERNAME=Enter_Your_Database_Username
DB_PASSWORD=Enter_Your_Database_Password
Now you have to run this migration by following the command:
php artisan migrate
Step 3: Create Auth Scaffold
Here, we will use laravel ui
package and create auth scaffold with the bootstrap framework. let's follow bellow command:
composer require laravel/ui
Now create a simple bootstrap auth system:
php artisan ui bootstrap --auth
And run npm i
and npm run dev
to compile javascript assets.
Step 4: Create Route
Here, we need to add one more route for the admin user home page so let's add that route in the web.php
file.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\ProfileController;
Route::middleware(['auth'])->group(function () {
Route::get('profile',[ProfileController::class,'index'])->name('profile');
Route::post('profile/{user}',[ProfileController::class,'update'])->name('profile.update');
});
Step 5: Create Controller
In this step, we need to create ProfileController and add the following code to that file:
app/Http/Controllers/Admin/ProfileController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Models\User;
use App\Http\Controllers\Controller;
use App\Http\Requests\ProfileUpdateRequest;
class ProfileController extends Controller
{
public function index()
{
return view('profile');
}
public function update(User $user, Request $request)
{
$user->update([
'name' => $request->name,
'email' => $request->email,
'updated_at' => now()
]);
return $this->success('profile','Profile updated successfully!');
}
}
Step 6: Create View
Now in this step, we will create a profile update form. So create it like:
resources/views/profile.blade.php
<form
id="formAccountSettings"
method="POST"
action="{{ route('profile.update',auth()->id()) }}"
enctype="multipart/form-data"
class="needs-validation"
role="form"
novalidate
>
@csrf
<div class="card-body">
<div class="row">
<div class="mb-3 col-md-6">
<label for="name" class="form-label">{{ trans('sentence.name')}}</label>
<input class="form-control" type="text" id="name" name="name" value="{{ auth()->user()->name }}" autofocus="" required>
<div class="invalid-tooltip">{{ trans('sentence.required')}}</div>
</div>
<div class="mb-3 col-md-6">
<label for="email" class="form-label">{{ trans('sentence.email')}}</label>
<input class="form-control" type="text" id="email" name="email" value="{{ auth()->user()->email }}" placeholder="john.doe@example.com">
<div class="invalid-tooltip">{{ trans('sentence.required')}}</div>
</div>
<div class="mt-2">
<button type="submit" class="button-create me-2">{{ trans('sentence.save_changes')}}</button>
</div>
</div>
</div>
</form>
Now all are set to go to check the laravel update user profile. So now you can test your app laravel update user data. Run the command and start server.
php artisan serve
Now you can test our application by visiting the below URL:
URL
Conclusion
Now we know laravel update user profile. Hope this update user laravel 9 tutorial will help you to create laravel 9 update user profile. You can test now how to update user profile laravel 9.