ホーム > Laravel > How to Update User Information in Laravel
Laravel

How to Update User Information in Laravel

Thank you for your continued support.
This article contains advertisements that help fund our operations.

⇨ Click here for the table of contents for Laravel articles

I have summarized how to update user information in Laravel.

Laravel Framework 8.83.8

Things to Do Next

There is a column named name in the users table,

We will write a process to update that name.

Prerequisites

Authentication (registration and login) is already set up

How to Update User Information

Add routing for the process to be added to web.php

Add it to web.php (This article is written in the Laravel 8 style. For Laravel 6 and earlier, please refer to this article).

use App\Http\Controllers\UserController;
Route::post('/user/update',[UserController::class,'update'])->name('user.update');

Create UserController

Create the file using the command.

php artisan make:controller UserController

UserController.php is generated.

Add to UserController

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;

class UserController extends Controller
{
    public function update(Request $request)
    {
        $user = Auth::user();
        dd($user);
    }
}

It's not complete, but I'll leave it like this for now.

Edit the display file (add to the page with the actual input form)

***.blade.php

<form method="POST" action="{{route('user.update')}}">
  @csrf
  <input name="name" />
  <button>Update</button>
</form>

Try pressing the newly created "Update" button!

If you see something like this on a black screen

App\Models\User {#996 ▼
  #fillable: array:3 [▶]
  #hidden: array:2 [▶]
  #casts: array:1 [▶]
  #connection: "mysql"
  #table: "users"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  ....
}

that's okay.

This is the output of

dd($user);

written in the Controller, proving that the process has reached this point.

Especially for beginners, it's important to first confirm that the function is working, as mistakes in routing specifications can sometimes mean that "the function itself is not working".

Update the Controller

Change the content of the function called update that you wrote earlier

    public function update(Request $request)
    {
        $user = Auth::user();
        dd($request->name);
    }

Now try entering some text and clicking the Update button.

If the text you entered appears on the screen, then it's okay.

<input name="name" />
<!-- The request named 'name' is passed to the Controller -->

Because 'name' is specified as "name" here, on the Laravel side, you can receive it as

$request->name;

Once all of these are working, all that's left is to write the update process!

    public function update(Request $request)
    {
        $user = Auth::user();
        $user->name = $request->name;
        $user->save();
        // return redirect()->route('');//Specify the page you want to redirect to after saving
    }

and that's it. If you make the actual changes and it works, then it's okay!

What NOT to Do

Trying to identify the user by parameter is not good

public function update($userId, Request $request)
{
  //This method is not good
  $user = User::find($userId);
  //and so on
}

Trying to identify the user by request parameter is not good

<input type="hidden" name="userId" />
public function update(Request $request)
{
  //This method is not good
  $user = User::find($request->userId);
  //and so on
}

These methods allow updating another user's information by simply changing the ID written in the browser, so always

Auth::id();
Auth::user();

to get the user information you want to update.

Summary

That's all.

I hope this can be helpful to someone.

For feedback or complaints, please contact me via Twitter DM.

That's all!

Popular Articles

Deploy PHP7.4 + Laravel6 Project to AWS EC2

Implement Breadcrumbs in Laravel with laravel-breadcrumbs Package

Please Provide Feedback
We would appreciate your feedback on this article. Feel free to leave a comment on any relevant YouTube video or reach out through the contact form. Thank you!