我正在使用“Laravel中的Eloquent ORM”执行数据库操作.我只想在数据库中获取最后一个插入ID(不是最大id).
我搜索了在laravel Eloquent ORM中获取最后一个插入ID,我得到以下链接(Laravel, get last insert id using Eloquent),这是指从以下函数“$data-> save()”获取最后一个插入ID.
但我需要得到
“型号::创建($数据)”;
我的查询:
GeneralSettingModel::create($GeneralData);
User::create($loginuserdata);
如何检索最后插入的ID?
解决方法:
“You may also use the create method to save a new model in a single
line. The inserted model instance will be returned to you from the
method. However, before doing so, you will need to specify either a
fillable or guarded attribute on the model, as all Eloquent models
protect against mass-assignment.
After saving or creating a new model that uses auto-incrementing IDs,
you may retrieve the ID by accessing the object’s id attribute:“
$insertedId = $user->id;
所以在你的样本中:
$user = User::create($loginuserdata);
$insertedId = $user->id;
然后在table2上它将会是
$input['table2_id'] = $insertedId;
table2::create($input);
标签:php,laravel,eloquent
来源: https://codeday.me/bug/20190928/1828972.html