laravel – How to get the current id of the record?


I am doing the user registration with Laravel scaffolding auth, and I need to get the id of that current register to do another operation, I am doing it as follows but I can't, it tells me that the $id variable is not defined: Undefined variable: id :

protected function create(array $data)
    return $id = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),




  • Right after creating the record invoke the ID
  • You get the recent ID through the latest method
  • You indicate the use of first so that it only returns a value
  • You access the ID as a property

A) Yes

$latestId = User::latest('id')->first()->id;

The latest() method will initially do a descending order by the created_at column, however if we do not have it or with it is not working, obtain the last desired id then we can modify a little by passing the id column as an argument of said method so we will tell it that give us a value ordering by the primary key.

This action will return you the ID of the last registration made.

Final edition, this would be the complete syntax to obtain the desired value:

    'propiedad1' => 'valor1',
    'propiedad2' => 'valor2',
return Modelo::latest()->first()->id;
Scroll to Top