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

我有两个模型之间的关系:终端和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关系.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...