0 votes
614 views
in MySQL by

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.

1 Answer

0 votes
by

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

$data->save();

$data->id;

Can be used like this.

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

For updated laravel version try this

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

Related questions

0 votes
1 answer 582 views
0 votes
1 answer 1.3k views
0 votes
1 answer 624 views
0 votes
1 answer 619 views
0 votes
0 answers 844 views
asked Feb 13, 2017 in Laravel by Simply
+1 vote
1 answer 1.5k views
asked Dec 7, 2016 in Laravel by Pinto
...