Update A User Profile In Laravel

Update A User Profile In Laravel

Step 1: Install Laravel 7 Project

Open your terminal and run the following command to create a new Laravel 7 project (not 5.7 as the heading mentioned):

composer create-project --prefer-dist laravel/laravel:^7.0 blog

Step 2: Set Up Database Connection

Update your .env file with the following database credentials:

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=management_system DB_USERNAME=root DB_PASSWORD=123456

Step 3: Define Update Route

In routes/web.php, add the route for updating the user:

// ------------------------ update ------------------------ // Route::post('userUpdate', 'HomeController@userUpdate');

Step 4: Create Controller Method

In app/Http/Controllers/HomeController.php, add the method:

use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; public function userUpdate(Request $request) { $userUpdate = [ 'id' => $request->idUpdate, 'name' => $request->name, 'email' => $request->email, 'department' => $request->department, 'division' => $request->division, 'role_name' => $request->roleName, ]; DB::table('users')->where('id', $request->idUpdate)->update($userUpdate); return redirect()->back()->with('userUpdate', 'User updated successfully.'); }

Step 5: Create Blade View

View path: resources/views/password/user.blade.php

Highlights:

  • The table lists users

  • Modal allows inline editing

  • Uses Bootstrap and jQuery

Key Features:

  1. User Table

  2. Update Modal with form

  3. JavaScript to auto-fill the form

Make sure your $userselect is passed from the controller.

Sample table row (simplified):

@foreach($userselect as $row) <tr> <td class="idUpdate">{{ $row->id }}</td> <td class="names">{{ $row->name }}</td> <td class="email">{{ $row->email }}</td> ... <td class="text-center"> <a class="m-r-15 text-muted userEdits" data-toggle="modal" data-target="#userUpdate"> <i class="icofont icofont-ui-edit" style="color: #2196f3;"></i> </a> </td> </tr> @endforeach

Modal form example:

<form action="/userUpdate" method="post"> @csrf <input type="hidden" id="idUpdate" name="idUpdate"> ... <input type="text" id="e_name" name="name" class="form-control"> ... </form>

JavaScript to fill modal with selected row's data:

$(document).on('click', '.userEdits', function() { var _this = $(this).closest('tr'); $('#idUpdate').val(_this.find('.idUpdate').text()); $('#e_name').val(_this.find('.names').text()); $('#e_email').val(_this.find('.email').text()); $('#e_department').val(_this.find('.department').text()); $('#e_division').val(_this.find('.division').text()); $('#e_roleName').val(_this.find('.roleName').text()); });


Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close