I'm currently using the below code to insert data in a table:
<?php
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);
}
}
I want to return the last ID inserted but I don't know how to get it.
Kind regards!
The shortest way is probably a call of the
refresh()
on the model:After saving model, the initialized instance has the id:
Using Eloquent Model
Using Query Builder
If the table has an auto-incrementing id, use the insertGetId method to insert a record and then retrieve the ID:
Refer: https://laravel.com/docs/5.1/queries#inserts
In laravel 5: you can do this: