<tfoot id='GIjZR'></tfoot><legend id='GIjZR'><style id='GIjZR'><dir id='GIjZR'><q id='GIjZR'></q></dir></style></legend>
    1. <small id='GIjZR'></small><noframes id='GIjZR'>

      • <bdo id='GIjZR'></bdo><ul id='GIjZR'></ul>

      1. <i id='GIjZR'><tr id='GIjZR'><dt id='GIjZR'><q id='GIjZR'><span id='GIjZR'><b id='GIjZR'><form id='GIjZR'><ins id='GIjZR'></ins><ul id='GIjZR'></ul><sub id='GIjZR'></sub></form><legend id='GIjZR'></legend><bdo id='GIjZR'><pre id='GIjZR'><center id='GIjZR'></center></pre></bdo></b><th id='GIjZR'></th></span></q></dt></tr></i><div id='GIjZR'><tfoot id='GIjZR'></tfoot><dl id='GIjZR'><fieldset id='GIjZR'></fieldset></dl></div>

      2. Laravel 获取每个组的最新记录

        时间:2023-09-22

          1. <legend id='KGmJX'><style id='KGmJX'><dir id='KGmJX'><q id='KGmJX'></q></dir></style></legend>

            <i id='KGmJX'><tr id='KGmJX'><dt id='KGmJX'><q id='KGmJX'><span id='KGmJX'><b id='KGmJX'><form id='KGmJX'><ins id='KGmJX'></ins><ul id='KGmJX'></ul><sub id='KGmJX'></sub></form><legend id='KGmJX'></legend><bdo id='KGmJX'><pre id='KGmJX'><center id='KGmJX'></center></pre></bdo></b><th id='KGmJX'></th></span></q></dt></tr></i><div id='KGmJX'><tfoot id='KGmJX'></tfoot><dl id='KGmJX'><fieldset id='KGmJX'></fieldset></dl></div>
            <tfoot id='KGmJX'></tfoot>

              <small id='KGmJX'></small><noframes id='KGmJX'>

                <bdo id='KGmJX'></bdo><ul id='KGmJX'></ul>
                  <tbody id='KGmJX'></tbody>

                  本文介绍了Laravel 获取每个组的最新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  I am trying to migrate some Raw SQL to an Eloquent (or Query Builder) scope on my model. My Parts history table looks like this:

                  +----+---------+--------+------------+
                  | 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 可以有多个状态相同的条目.

                  Notice the same part_id can have multiple entries where the status is the same.

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

                  At the moment I use the following to select the latest status:

                  $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( ... )
                  

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

                  I am trying to make a scope on the parts model out of this, so far I have this:

                  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),我就是想不通其余的

                  which is part way there (but still using some raw SQL), I just can't figure out the rest

                  推荐答案

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

                  You could rewrite your query as left join to get the same results

                  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
                  

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

                  and I guess you can transform easily above query in your scope something like

                  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 选择具有最大值的所有行created_at

                  Laravel - 获取最后一个条目每种 UID 类型

                  Laravel Eloquent 按最近记录分组

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

                  Edit using above query as has relation,To get the latest history for each part you can define a hasOne relation like

                  namespace AppModels;
                  
                  use IlluminateDatabaseEloquentModel;
                  use IlluminateSupportFacadesDB;
                  class Part extends Model
                  {
                      public function latest_history()
                      {
                          return $this->hasOne(AppModelsPartHistory::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.*');
                      }
                  }
                  

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

                  And then to load parts with their latest history you could eager load above defined mapping as

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

                  您将拥有一份零件清单以及最新的历史记录

                  You will have a list of parts along with latest history as

                  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
                                  )
                  
                          )
                  ....
                  )
                  

                  这篇关于Laravel 获取每个组的最新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:用户注册时自动创建个人资料(Laravel 5) 下一篇:Laravel 中关系的计数关系

                  相关文章

                  最新文章

                    <tfoot id='mELlq'></tfoot>
                    1. <legend id='mELlq'><style id='mELlq'><dir id='mELlq'><q id='mELlq'></q></dir></style></legend>
                      • <bdo id='mELlq'></bdo><ul id='mELlq'></ul>
                      <i id='mELlq'><tr id='mELlq'><dt id='mELlq'><q id='mELlq'><span id='mELlq'><b id='mELlq'><form id='mELlq'><ins id='mELlq'></ins><ul id='mELlq'></ul><sub id='mELlq'></sub></form><legend id='mELlq'></legend><bdo id='mELlq'><pre id='mELlq'><center id='mELlq'></center></pre></bdo></b><th id='mELlq'></th></span></q></dt></tr></i><div id='mELlq'><tfoot id='mELlq'></tfoot><dl id='mELlq'><fieldset id='mELlq'></fieldset></dl></div>

                      <small id='mELlq'></small><noframes id='mELlq'>