[英]how can i get last inserted id in laravel 8 using elequent model
i want to get the last iserted id in my table to affect it into an other table i've tried thosz but it doesnt work getPdo()->lastInsertId(), also lastInsertId()我想在我的表中获取最后一个插入的 id 以将其影响到我尝试过 thosz 的另一个表中,但它不起作用 getPdo()->lastInsertId(),还有 lastInsertId()
public function create()
{
$demande_inscription=Demande_inscription::create([
'id_demandeur' => Demandeur::getPdo()->lastInsertId(),
]);
return response()->json([$demande_inscription],201);
}
if you are using eloquent then the object of the class that is used to create/save record is having the id of the record you inserted, let me explain it below如果您使用的是 eloquent 则用于创建/保存记录的 class 的 object 具有您插入的记录的 ID,让我在下面解释一下
$object = new Model();
$object->DBfield = Data;
$object->save();
// After saving your record
$object->id;
For Laravel适用于 Laravel
$user = new User();
$user->name = 'Rakesh';
$user->save();
//Getting Last inserted id
$insertedId = $user->id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.