Retrieve last insert id by Eloquent-Laravel

 We know how to retrieve  last inserted id using raw query but there are different ways to get last inserted id using Eloquent.

Here is an example:

public function saveDetailsCompany()

{

    $post = Input::All();

    $data = new Company;

    $data->name = $post['name'];

    $data->address = $post['address'];

    $data->type = $post['type'];

    $data->fecha_created = date("Y-m-d H:i:s");

    $data->fecha_modified = date("Y-m-d H:i:s");

    if ($data->save()) {

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

    }

}

Another example for getting last insert id when using DB::insert is shown below.

DB::getPdo()->lastInsertId();

You can also use insertGetId method and store it in a variable and  retrieve later when needed.

$id = DB::table('users')->insertGetId([

    'email' => 'john@example.com',

    'votes' => 0

]);