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
.
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
.
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;
DB::Query('SELECT LAST_INSERT_ID()');
According to this post here: laravel raw queries last_insert_id
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.