How to retrieve the ID of the last record inserted in the bank? [closed]

-1

How do I retrieve the id of the last record made in a table in the database?

I'm using Laravel. Before using a framework I used LASTINSERT .

    
asked by anonymous 18.04.2014 / 19:09

3 answers

5

If you use Eloquent, this is done automatically, regardless of the database system:

class Post extends Eloquent {

}

$post = Post::create(['title' => 'Laravel']);

echo $post->id;
    
09.05.2014 / 21:38
2
DB::Query('SELECT LAST_INSERT_ID()');

According to this post here: laravel raw queries last_insert_id

    
18.04.2014 / 19:25
2

If you are using PDO in Laravel the correct form is:

$id = DB::connection('mysql')->pdo->lastInsertId();

If the database is not MySQL, change this in the code above.

    
19.04.2014 / 01:40