【问题标题】:Laravel get latest record for each groupLaravel 获取每个组的最新记录
【发布时间】:2018-11-22 16:28:14
【问题描述】:

我正在尝试将一些原始 SQL 迁移到我模型上的 Eloquent(或查询生成器)范围。我的零件历史表如下所示:

+----+---------+--------+------------+
| id | part_id | status | created_at |
+----+---------+--------+------------+
|  1 |       1 |      1 | ...        |
|  2 |       1 |      2 | ...        |
|  3 |       2 |      1 | ...        |
|  4 |       1 |      2 | ...        |
|  5 |       2 |      2 | ...        |
|  6 |       1 |      3 | ...        |

请注意,同一个 part_id 可以有多个状态相同的条目。

目前我使用以下选择最新状态:

$part = Part::leftjoin( DB::raw("
 (SELECT t1.part_id, ph.status, t1.part_status_at 
  FROM (
    SELECT part_id, max(created_at) part_status_at
    FROM part_histories
    GROUP BY part_id) t1 
  JOIN part_histories ph ON ph.part_id = t1.part_id AND t1.part_status_at = ph.created_at) as t2
  )", 't2.part_id', '=', 'parts.id')->where( ... )

我正在尝试以此为零件模型制作一个范围,到目前为止我有这个:

public function scopeWithLatestStatus($query)
{
    return $query->join(DB::raw('part_histories ph'), function ($join) {
         $join->on('ph.part_id', '=', 't1.id')->on('t1.part_status_at', '=', 'ph.created_at');
      })
      ->from(DB::raw('(select part_id as id, max(created_at) part_status_at from part_histories GROUP BY part_id) t1'))
      ->select('t1.id', 'ph.part_status', 't1.part_status_at');
}

这是其中的一部分(但仍然使用一些原始 SQL),我只是想不通其余的

【问题讨论】:

  • 只是出于好奇,为什么?如果原始 SQL 工作正常,那么切换到 ORM 有什么好处?我知道这与问题无关,我只是好奇。
  • 好点,戈登 :-) 主要是因为我正在将其他东西更改为 ORM。事实证明,我决定坚持使用原始 SQL,因为我并没有很好地解释我的问题。

标签: php mysql laravel eloquent greatest-n-per-group


【解决方案1】:

您可以将查询重写为左连接以获得相同的结果

select a.* 
from part_histories a
left join part_histories b on a.part_id = b.part_id 
                            and a.created_at < b.created_at
where b.part_id is null

我猜你可以在你的范围内轻松转换上面的查询,比如

public function scopeWithLatestStatus($query)
{
    return $query->leftJoin('part_histories as b', function ($join) {
                $join->on('a.part_id', '=', 'b.part_id')
                     ->where('a.created_at', '<', 'b.created_at');
            })
        ->whereNull('b.part_id')
        ->from('part_histories as a')
        ->select('a.*');
}

Laravel Eloquent select all rows with max created_at

Laravel - Get the last entry of each UID type

Laravel Eloquent group by most recent record

使用上述查询作为has 关系进行编辑,要获取每个部分的最新历史记录,您可以定义hasOne 关系,如

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Part extends Model
{
    public function latest_history()
    {
        return $this->hasOne(\App\Models\PartHistory::class, 'part_id')
            ->leftJoin('part_histories as p1', function ($join) {
                $join->on('part_histories.part_id', '=', 'p1.part_id')
                    ->whereRaw(DB::raw('part_histories.created_at < p1.created_at'));
            })->whereNull('p1.part_id')
            ->select('part_histories.*');
    }
}

然后要加载具有最新历史记录的部件,您可以将上述定义的映射加载为

$parts = Part::with('latest_history')->get();

您将获得零件列表以及最新历史记录

Array
(
    [0] => Array
        (
            [id] => 1
            [title] => P1
            [latest_history] => Array
                (
                    [id] => 6
                    [created_at] => 2018-06-16 08:25:10
                    [status] =>  1
                    [part_id] => 1
                )

        )
....
)

【讨论】:

  • 谢谢,但我的错,因为我没有把我的问题说得足够清楚——你提供的东西很好用——但我已经有了这个工作,尽管使用了不同的方法。我应该问的是如何更改范围方法以将最新状态附加到任何 ORM 部分查询:Part::withLatestStatus()->get()
  • @rareclass 您想从您的零件模型中加载此数据吗?而不是应用范围
  • 我想应用范围而不是使用属性,以避免在检索部件集合时出现 n+1 问题。
  • @rareclass 我想如果您可以将此查询定义为hasOne 并进行急切加载,那么就不会有 n+1 的机会。让我知道这是否有意义,然后我会更新我的答案
  • 有趣,我什至不知道你可以通过 haseOne 关系做到这一点
猜你喜欢
  • 1970-01-01
  • 2017-02-05
  • 2016-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-07
  • 1970-01-01
相关资源
最近更新 更多