php取数据库最后一个,php – 如何在Eloquent ORM laravel中获取最后一个插入ID

我正在使用“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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值