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

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

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

      合并和排序兩個 Eloquent 集合?

      Merge and Sort two Eloquent Collections?(合并和排序兩個 Eloquent 集合?)
      <legend id='9Owo8'><style id='9Owo8'><dir id='9Owo8'><q id='9Owo8'></q></dir></style></legend>

      1. <i id='9Owo8'><tr id='9Owo8'><dt id='9Owo8'><q id='9Owo8'><span id='9Owo8'><b id='9Owo8'><form id='9Owo8'><ins id='9Owo8'></ins><ul id='9Owo8'></ul><sub id='9Owo8'></sub></form><legend id='9Owo8'></legend><bdo id='9Owo8'><pre id='9Owo8'><center id='9Owo8'></center></pre></bdo></b><th id='9Owo8'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='9Owo8'><tfoot id='9Owo8'></tfoot><dl id='9Owo8'><fieldset id='9Owo8'></fieldset></dl></div>
        <tfoot id='9Owo8'></tfoot>
          <tbody id='9Owo8'></tbody>

      2. <small id='9Owo8'></small><noframes id='9Owo8'>

          <bdo id='9Owo8'></bdo><ul id='9Owo8'></ul>

                本文介紹了合并和排序兩個 Eloquent 集合?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                我有兩個集合,我想將它合并到一個變量中(當(dāng)然,按一列排序 - created_at).我該怎么做?

                I've two Collections and I want merge it to one variable (of course, with ordering by one collumn - created_at). How Can I do that?

                我的控制器看起來像:

                $replies = Ticket::with('replies', 'replies.user')->find($id);
                $logs = DB::table('logs_ticket')
                        ->join('users', 'users.id', '=', 'mod_id')
                        ->where('ticket_id', '=', $id)
                        ->select('users.username', 'logs_ticket.created_at', 'action')
                        ->get();
                

                我的輸出查找例如:

                回復(fù):

                ID | ticket_id | username | message | created_at
                1  | 1         | somebody | asdfghj | 2014-04-12 12:12:12
                2  | 1         | somebody | qwertyi | 2014-04-14 12:11:10
                

                日志:

                ID | ticket_id | username | action | created_at
                1  | 1         | somebody | close  | 2014-04-13 12:12:14
                2  | 1         | somebody |  open  | 2014-04-14 14:15:10
                

                我想要這樣的東西:

                ticket_id | table | username | message | created_at
                1         |replies| somebody | asdfghj | 2014-04-12 12:12:12
                1         | logs  | somebody |  close  | 2014-04-13 12:12:14
                1         | logs  | somebody |  open   | 2014-04-14 11:15:10
                1         |replies| somebody | qwertyi | 2014-04-14 12:11:10
                

                我的票證模型看起來是這樣的:

                My Ticket Model looks that:

                <?php
                
                class Ticket extends Eloquent {
                
                    protected $table = 'tickets';
                
                    public function replies() {
                        return $this->hasMany('TicketReply')->orderBy('ticketreplies.created_at', 'desc');
                    }
                
                    public function user()
                    {
                        return $this->belongsTo('User');
                    }
                }
                ?>
                

                推薦答案

                你不會很容易得到你想要的東西.

                You're not going to be able to get exactly what you want easily.

                通常,使用 $collection->merge($otherCollection); 合并應(yīng)該很容易,并使用 $collection->sort(); 進(jìn)行排序.但是,由于沒有唯一 ID 和所需的表格"列,合并將無法按照您希望的方式進(jìn)行,您必須手動進(jìn)行.

                In general, merging should be easy with a $collection->merge($otherCollection);, and sort with $collection->sort();. However, the merge won't work the way you want it to due to not having unique IDs, and the 'table' column that you want, you'll have to make happen manually.

                此外,它們實(shí)際上都是不同類型的集合我認(rèn)為(基于 EloquentModel 的集合將是 EloquentCollection,另一個是標(biāo)準(zhǔn)的 Collection),這可能會導(dǎo)致其自身的問題.因此,我建議對兩者都使用 DB::table(),并使用您可以控制的列來擴(kuò)充您的結(jié)果.

                Also they are actually both going to be collections of different types I think (the one being based on an EloquentModel will be EloquentCollection, and the other being a standard Collection), which may cause its own issues. As such, I'd suggest using DB::table() for both, and augmenting your results with columns you can control.

                至于實(shí)現(xiàn)這一點(diǎn)的代碼,我不確定,因?yàn)槲覜]有在 Laravel 中做很多低級數(shù)據(jù)庫工作,所以不知道創(chuàng)建查詢的最佳方法.無論哪種方式,僅僅因?yàn)橥ㄟ^兩個查詢和一些 PHP 合并來管理它看起來開始很痛苦,我建議在一個數(shù)據(jù)庫查詢中完成所有操作.它實(shí)際上看起來更整潔,可以說更易于維護(hù):

                As for the code to achieve that, I'm not sure as I don't do a lot of low-level DB work in Laravel, so don't know the best way to create the queries. Either way, just because it's looking like starting to be a pain to manage this with two queries and some PHP merging, I'd suggest doing it all in one DB query. It'll actually look neater and arguably be more maintainable:

                你需要的 SQL 是這樣的:

                The SQL you'll need is something like this:

                SELECT * FROM
                (
                    SELECT
                        `r`.`ticket_id`,
                        'replies' AS `table`,
                        `u`.`username`,
                        `r`.`message`,
                        `r`.`created_at`
                    FROM `replies` AS `r`
                    LEFT JOIN `users` AS `u`
                        ON `r`.`user_id` = `u`.`id`
                    WHERE `r`.`ticket_id` = ?
                ) UNION (
                    SELECT
                        `l`.`ticket_id`,
                        'logs' AS `table`,
                        `u`.`username`,
                        `l`.`action` AS `message`,
                        `l`.`created_at`
                    FROM `logs` AS `l`
                    LEFT JOIN `users` AS `u`
                        ON `l`.`user_id` = `u`.`id`
                    WHERE `l`.ticket_id` = ?
                )
                ORDER BY `created_at` DESC
                

                這是不言自明的:執(zhí)行兩個查詢,返回相同的列,UNION 它們,然后在 MySQL 中對結(jié)果集進(jìn)行排序.希望它(或類似的東西,因?yàn)槲也坏貌徊聹y您的數(shù)據(jù)庫結(jié)構(gòu))對您有用.

                It's pretty self-explanatory: do the two queries, returning the same columns, UNION them and then sort that result set in MySQL. Hopefully it (or something similar, as I've had to guess your database structure) will work for you.

                至于將其轉(zhuǎn)換為 Laravel DB:: 樣式的查詢,我想這取決于您.

                As for translating that into a Laravel DB::-style query, I guess that's up to you.

                這篇關(guān)于合并和排序兩個 Eloquent 集合?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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() 是從整個服務(wù)器還是從同一用戶獲取記錄?)
                PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數(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='bwKID'></tfoot>
                <legend id='bwKID'><style id='bwKID'><dir id='bwKID'><q id='bwKID'></q></dir></style></legend>
                <i id='bwKID'><tr id='bwKID'><dt id='bwKID'><q id='bwKID'><span id='bwKID'><b id='bwKID'><form id='bwKID'><ins id='bwKID'></ins><ul id='bwKID'></ul><sub id='bwKID'></sub></form><legend id='bwKID'></legend><bdo id='bwKID'><pre id='bwKID'><center id='bwKID'></center></pre></bdo></b><th id='bwKID'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='bwKID'><tfoot id='bwKID'></tfoot><dl id='bwKID'><fieldset id='bwKID'></fieldset></dl></div>

                    <tbody id='bwKID'></tbody>
                      <bdo id='bwKID'></bdo><ul id='bwKID'></ul>
                    • <small id='bwKID'></small><noframes id='bwKID'>

                        • 主站蜘蛛池模板: 酒色成人网 | 精品日韩一区 | 五月婷婷丁香 | 久久婷婷av | 婷婷国产一区 | 欧美一区二区三区精品免费 | 全免费a级毛片免费看视频免 | 中文字幕免费中文 | 亚洲综合色自拍一区 | 亚洲精品一区二区二区 | 99色综合 | 国产精品高潮呻吟久久久久 | 日韩综合在线播放 | 国产精品毛片av一区 | 午夜视频精品 | 男人天堂视频在线观看 | 日韩精品一区二区三区在线播放 | 91麻豆精品一区二区三区 | 日韩视频国产 | 在线视频a | 精品免费视频一区二区 | 中文字幕在线播放第一页 | 亚洲成人自拍 | 一区二区av | 成人在线观看免费爱爱 | 中文字幕日韩三级 | 红色av社区 | 国产欧美一区二区精品忘忧草 | 99色综合| 国产一区二区三区在线免费观看 | 在线观看免费国产 | 精品国产1区2区3区 在线国产视频 | 成人不卡视频 | 黄色在线免费观看视频网站 | 天天干国产 | 亚洲一区国产 | 91精品中文字幕一区二区三区 | 欧美成人精品欧美一级 | 欧美一区2区三区4区公司二百 | 亚洲在线一区 | 欧美精品中文字幕久久二区 |