【问题标题】:Laravel Eloquent - get last insert of related modelLaravel Eloquent - 获取相关模型的最后插入
【发布时间】:2016-12-19 03:24:07
【问题描述】:

我有两个模型之间的关系:TerminalterminalRent

一个终端可以有许多终端租金。 我想获得特定终端的最后一次租金。

$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

【问题讨论】:

  • 你使用的是什么版本的 Laravel?
  • @Adrenaxus 我正在使用 5.2

标签: php laravel eloquent laravel-5.2


【解决方案1】:

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

为了获得更好的性能,试试这个

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

【讨论】:

    【解决方案2】:

    试试这个代码

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

    【讨论】:

      【解决方案3】:

      使用它来获取特定终端的最后租金:

      $terminal = Terminal::find($id); //the terminal for which you want to get the last rent
      $lastRent = $terminal->rentals()->orderBy('id', 'DESC')->first();
      

      请记住,这只是一种方法,可能还有其他选项更适合您的需求。

      【讨论】:

      • 什么是rentals()?那应该是 TerminalRent-Model 吗?
      • rentals()rents()(我不确定拼写)将是终端模型中定义的终端和租金之间的关系:public function rents(){return $this-&gt;hasMany('Rent');}
      • 啊,好的,谢谢。可悲的是,这也不起作用,因为我正在返回其他终端的行。
      • 请具体说明什么不起作用。我用我的 1:n 关系对其进行了测试,它确实确​​实有效。
      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        如果 Terminal 和 Terminal rent 之间的关系是 hasMany 你可以这样做:

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

        确保您最后一次通过dd($lastRented);租用

        告诉我你的进展情况

        编辑:如果这给了您不正确的 terminalRent 实例,请尝试 first();我不记得 Eloquent 在默认情况下是如何订购热切的关系的。

        【讨论】:

        • 这就像一个魅力:$lastRent = Terminal::with('terminalRent')-&gt;where('id', $id)-&gt;orderBy('id', 'desc')-&gt;first(); $lastRented = $lastRent-&gt;TerminalRent-&gt;first(); 我只是不知道为什么,因为我使用了两次 first(),这有点令人困惑。你能解释一下吗?谢谢
        • 当然——所以,我们有两种常用的完成查询的方法,get();和第一(); - 运行 get();将返回一个集合,其中首先从该查询返回一个对象,在您的情况下,关系 TerminalRent 通过调用 first() 返回一个集合;您获得该对象的第一个实例。你可以通过访问 Laravel 文档收集方法并测试其中的任何方法来证明它是一个集合。
        • 在你的情况下,你可以这样做:$lastRent = Terminal::with('terminalRent')-&gt;where('id', $id)-&gt;get(); $lastRented = $lastRent-&gt;TerminalRent-&gt;first(); :)
        • @Maraboc 这不起作用Undefined property: Illuminate\Database\Eloquent\Collection::$TerminalRent 但是Terminal´Rent 存在,因为我已经在使用它了,哈哈。无论如何谢谢 :) @excojish 谢谢!
        • 试试这个:$lastRent = Terminal::with('terminalRent')-&gt;where('id', $id)-&gt;get(); $lastRented = $lastRent-&gt;TerminalRent()-&gt;orderBy('id', 'DESC')-&gt;first();!
        猜你喜欢
        • 1970-01-01
        • 2014-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-12
        • 2017-09-21
        • 2019-10-10
        相关资源
        最近更新 更多