Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel雄辩-获取相关模型的最后插入_Php_Laravel_Eloquent_Laravel 5.2 - Fatal编程技术网

Php Laravel雄辩-获取相关模型的最后插入

Php Laravel雄辩-获取相关模型的最后插入,php,laravel,eloquent,laravel-5.2,Php,Laravel,Eloquent,Laravel 5.2,我在两个模型之间有一种关系:Terminal和terminalRent 一个终端可以有许多终端租金。 我想拿到一个特定航站楼的最后租金 $lastRent = Terminal::with('terminalRent')->where('id', $id)->orderBy('id', 'desc')->first(); 使用where()时,无论我想获取与特定终端相关的终端租金的最后一条记录,它都会反映模式上的记录$id是终端的id,表的连接方式如下: Terminal -

我在两个模型之间有一种关系:
Terminal
terminalRent

一个终端可以有许多终端租金。 我想拿到一个特定航站楼的最后租金

$lastRent = Terminal::with('terminalRent')->where('id', $id)->orderBy('id', 'desc')->first();
使用
where()
时,无论我想获取与特定终端相关的终端租金的最后一条记录,它都会反映
模式上的记录
$id
是终端的id,表的连接方式如下:

Terminal
----
ID


terminalRent
-----
ID
terminal_id <--- this is the foreign key for the ID of the Terminal Table
终端
----
身份证件
末期租金
-----
身份证件

航站楼id如果航站楼和航站楼租金之间的关系非常密切,您可以执行以下操作:

$lastRented = $lastRent->terminalRent->last();
通过
dd($lastreated)确保您拥有最后一次租用

让我知道你进展如何


编辑:如果这提供了terminalRent的不正确实例,请首先尝试();我不记得雄辩的命令在默认情况下是如何破坏关系的。

您可以加入它们,然后根据
terminalRent.id
对结果进行排序,然后选择第一个:

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

使用此选项可获取特定航站楼的最后租金:

$terminal = Terminal::find($id); //the terminal for which you want to get the last rent
$lastRent = $terminal->rentals()->orderBy('id', 'DESC')->first();
请记住,这只是一种方法,可能还有其他更适合您需要的选项。

试试这段代码

$lastRent =  Terminal::with(['terminalRent' => function ($query) { 
                        $query->orderBy('id', 'desc')->first();
                }]);

@Parithiban解决方案非常有用,但此函数运行一个查询两次

要获得更好的性能,请尝试此选项

$lastRent=Terminal::with(['terminalRent'=>函数($query){
返回$query->orderBy('id','desc')->limit(1);
}]);

您使用的是什么版本的Laravel?@Adrenaxus我正在使用5.2这就像一个符咒:
$lastRent=Terminal::with('terminalRent')->where('id',$id)->orderBy('id','desc')->first()$lastRent=$lastRent->TerminalRent->first()我只是不知道为什么,因为我使用了两次first(),有点混乱。你能解释一下吗?当然要感谢——因此,我们有两种常用的完成查询的方法,get();首先()运行get();将返回一个集合,其中首先从该查询返回单个对象,在您的情况下,关系TerminalRent通过调用first()返回集合;您将获得该对象的第一个实例。您可以通过访问Laravel文档收集方法并测试其中的任何方法来证明它是一个集合。在您的情况下,您可以这样做:
$lastRent=Terminal::with('terminalRent')->where('id',$id)->get()$lastRent=$lastRent->TerminalRent->first()
:)@Maraboc这不起作用
未定义的属性:Illumb\Database\Eloquent\Collection::$TerminalRent
但是Terminal'Rent存在,因为我已经在使用它了,哈哈。无论如何谢谢:)@excojish谢谢!试试这个:
$lastRent=Terminal::with('terminalRent')->where('id',$id)->get()$lastRent=$lastRent->TerminalRent()->orderBy('id','DESC')->first()!什么是
rentals()
?这应该是终端租金模型吗?
rentals()
rents()
(我不确定拼写)将是终端和租金之间的关系,如终端模型中定义的:
public function rents(){return$this->hasMany('Rent');}
啊,好的,谢谢。遗憾的是,这也不起作用,因为我得到的是其他终端的返回行。请具体说明什么不起作用。我用我的1:n关系测试了它,它确实有效。