Answers for "how to get last inserted collection in laravel eloquent"

PHP
1

how to get the Last Inserted Id in laravel

//if you have used save method like this
$data->save();
//use this to get last row id;
$lastRowId = $data->id;

//For other insert methods get last inserted id like below
    
    //Using create() method
    $book = Book::create(['name'=>'Laravel Warrior']);
    $lastId = $book->id;

    //Using insertGetId()
    $id = DB::table('books')->insertGetId( ['name' => 'Laravel warrior'] );   
    $lastId = $id;

    //Using lastInsertId() method
    $lastId = DB::getPdo()->lastInsertId();
Posted by: Guest on April-09-2022
2

return last inserted id in laravel

$id = DB::table('users')->insertGetId([
    'email' => '[email protected]',
    'votes' => 0
]);
Posted by: Guest on April-05-2021

Code answers related to "how to get last inserted collection in laravel eloquent"

Browse Popular Code Answers by Language