Search code examples
phpdatabaselaravellast-insert-id

Get the id of the last saved entry


Save gives only true or false.

$query = (new Item())->fill([
    'first'=>$first,
    'second'=>$second
])->save();
$lastInsertId = $query->id;
//does not work...
return ['status'=>true];

Solution

  • Use createfunction instead.

    $item = Item::create([
        'first' => $first,
        'second' => $second
    ]);
    
    $id = $item->id;