加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – Laravel Eloquent – 获取相关模型的最后一个插入

发布时间:2020-12-14 19:35:08 所属栏目:大数据 来源:网络整理
导读:我有两个模型之间的关系:终端和terminalRent. 终端可以有许多终端租赁. 我想获得特定终端的最后租金. $lastRent = Terminal::with('terminalRent')-where('id',$id)-orderBy('id','desc')-first(); 当使用where()时,它将反映Modal上的那些我希望获得与特定
我有两个模型之间的关系:终端和terminalRent.

终端可以有许多终端租赁.
我想获得特定终端的最后租金.

$lastRent = Terminal::with('terminalRent')->where('id',$id)->orderBy('id','desc')->first();

当使用where()时,它将反映Modal上的那些我希望获得与特定终端相关的terminalRent的最后一条记录. $id是终端的Id,表连接如下:

Terminal
----
ID


terminalRent
-----
ID
terminal_id <--- this is the foreign key for the ID of the Terminal Table

解决方法

如果终端和终端租金之间的关系很多,你可以这样做:

$lastRented = $lastRent->terminalRent->last();

确保你最后通过dd($lastRented)租借;

让我知道你是怎么办的

编辑:如果这给你不正确的terminalRent实例,请尝试first();我不记得Eloquent默认如何命令eagerlaoded关系.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读