久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  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 class="qwawimqqmiuu" 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>
    1. Laravel 獲取每個(gè)組的最新記錄

      Laravel get latest record for each group(Laravel 獲取每個(gè)組的最新記錄)

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

              <tbody id='GIjZR'></tbody>

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

                <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 class="qwawimqqmiuu" id='GIjZR'><tfoot id='GIjZR'></tfoot><dl id='GIjZR'><fieldset id='GIjZR'></fieldset></dl></div>

              1. 本文介紹了Laravel 獲取每個(gè)組的最新記錄的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                我正在嘗試將一些原始 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 | ...        |
                

                請注意,同一個(gè) part_id 可以有多個(gè)狀態(tài)相同的條目.

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

                目前我使用以下選擇最新狀態(tài):

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

                我正在嘗試在零件模型上制作一個(gè)范圍,到目前為止我有這個(gè):

                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

                推薦答案

                您可以將查詢重寫為左連接以獲得相同的結(jié)果

                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
                

                我猜你可以在你的范圍內(nèi)輕松轉(zhuǎn)換上面的查詢,比如

                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 - 獲取最后一個(gè)條目每種 UID 類型

                Laravel Eloquent 按最近記錄分組

                使用上述查詢作為 has 關(guān)系進(jìn)行編輯,要獲取每個(gè)部分的最新歷史記錄,您可以定義一個(gè) hasOne 關(guān)系,如

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

                這篇關(guān)于Laravel 獲取每個(gè)組的最新記錄的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                相關(guān)文檔推薦

                MySQLi prepared statement amp; foreach loop(MySQLi準(zhǔn)備好的語句amp;foreach 循環(huán))
                Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個(gè)服務(wù)器還是從同一用戶獲取記錄?)
                PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個(gè)參數(shù))
                Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結(jié)果填充變量)
                MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“l(fā)ocalhost的訪問被拒絕)

                      <tfoot id='mELlq'></tfoot>
                          <tbody id='mELlq'></tbody>

                        <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 class="qwawimqqmiuu" id='mELlq'><tfoot id='mELlq'></tfoot><dl id='mELlq'><fieldset id='mELlq'></fieldset></dl></div>

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

                          主站蜘蛛池模板: 伊人网一区 | 狠狠爱综合| 免费黄色片在线观看 | 国产欧美一区二区三区在线看 | a亚洲精品 | 国产色婷婷精品综合在线手机播放 | 亚洲成人黄色 | 日本污视频 | 国产视频1 | 欧洲一区视频 | caoporn免费 | 欧美一区二区三区在线 | 天天天堂 | 二区在线视频 | 99re国产视频 | 最新中文字幕一区 | 成人永久免费视频 | 亚洲一区二区三区在线播放 | 国产亚洲日本精品 | 欧美一区二区三区在线 | 一级欧美 | 亚洲精品电影在线观看 | 成人亚洲精品 | 成人影 | 91久久精品一区二区二区 | 欧美日韩精品一区二区天天拍 | www.日日夜夜 | 国产 欧美 日韩 一区 | 97精品视频在线 | 乱码av午夜噜噜噜噜动漫 | 无码日韩精品一区二区免费 | 久久国产香蕉 | 日韩视频在线一区二区 | www.国产91 | 精品视频www | 日韩 欧美 综合 | 久久久精品在线 | 电影午夜精品一区二区三区 | 在线看黄免费 | 日韩中文字幕久久 | 在线成人|