SoldierCorp SoldierCorp - 6 months ago 561
PHP Question

Laravel, get last insert id using Eloquent

I'm currently using this code to insert data in a table:

public function saveDetailsCompany()
{
$post = Input::All();

$data = new Company;

$data->nombre = $post['name'];
$data->direccion = $post['address'];
$data->telefono = $post['phone'];
$data->email = $post['email'];
$data->giro = $post['type'];
$data->fecha_registro = date("Y-m-d H:i:s");
$data->fecha_modificacion = date("Y-m-d H:i:s");

if($data->save()) {
return Response::json(array('success' => true), 200);
}
}


And I want to return the last ID inserted but I don't know how to get it.

Kind regards!

Answer

After save, $data->id should be the last id inserted.

return Response::json(array('success' => true, 'last_insert_id' => $data->id), 200);