当前位置: 动力学知识库 > 问答 > 编程问答 >

php - How to get last inserted id in Laravel?

问题描述:

Here is my code

 $users = DB::table('users')->insert(array(

'email_id' => $email_id,

'name' => $name,

));

$lastInsertedID = $users->lastInsertId();

return $lastInsertedID;

I want to get last inserted id for particular record. I have tried using Eloquent ORM. but it didn't work.

Any help would be grateful.

Thank You.

网友答案:

Use insertGetId()

$id = DB::table('users')-> insertGetId(array(
        'email_id' => $email_id,
        'name' => $name,
));
网友答案:

Follow the code to get last inserted id by using ->insert()

$lastInsertedID = DB::table('users')
                     ->insert( array(
                                   'email_id' => $email_id,
                                   'name' => $name
                               )
                     )
                    ->lastInsertId();
分享给朋友:
您可能感兴趣的文章:
随机阅读: