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

      <tfoot id='pT0ES'></tfoot>
        <bdo id='pT0ES'></bdo><ul id='pT0ES'></ul>

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

    2. <i id='pT0ES'><tr id='pT0ES'><dt id='pT0ES'><q id='pT0ES'><span id='pT0ES'><b id='pT0ES'><form id='pT0ES'><ins id='pT0ES'></ins><ul id='pT0ES'></ul><sub id='pT0ES'></sub></form><legend id='pT0ES'></legend><bdo id='pT0ES'><pre id='pT0ES'><center id='pT0ES'></center></pre></bdo></b><th id='pT0ES'></th></span></q></dt></tr></i><div id='pT0ES'><tfoot id='pT0ES'></tfoot><dl id='pT0ES'><fieldset id='pT0ES'></fieldset></dl></div>
    3. Laravel Eloquent 按关系表列排序

      时间:2023-09-24

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

    4. <legend id='EHHhp'><style id='EHHhp'><dir id='EHHhp'><q id='EHHhp'></q></dir></style></legend>
          • <bdo id='EHHhp'></bdo><ul id='EHHhp'></ul>
              <tbody id='EHHhp'></tbody>

                <tfoot id='EHHhp'></tfoot>

                <i id='EHHhp'><tr id='EHHhp'><dt id='EHHhp'><q id='EHHhp'><span id='EHHhp'><b id='EHHhp'><form id='EHHhp'><ins id='EHHhp'></ins><ul id='EHHhp'></ul><sub id='EHHhp'></sub></form><legend id='EHHhp'></legend><bdo id='EHHhp'><pre id='EHHhp'><center id='EHHhp'></center></pre></bdo></b><th id='EHHhp'></th></span></q></dt></tr></i><div id='EHHhp'><tfoot id='EHHhp'></tfoot><dl id='EHHhp'><fieldset id='EHHhp'></fieldset></dl></div>
                本文介绍了Laravel Eloquent 按关系表列排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我尝试通过 shop_products_options 表中的 pinned 列对 shop_products 表中的产品进行排序:

                I tried to sort products from shop_products table by pinned column from shop_products_options table:

                $products = ShopProduct::with(['options' => function ($query) {
                
                    $query->orderBy('pinned', 'desc'); 
                
                }])->paginate(5);
                

                我在 ShopProduct 模型中设置了关系:

                I set relation in ShopProduct model:

                public function options()
                {
                    return $this->hasOne('ShopOptions');
                }
                

                但是产品没有排序.我得到一个仅适用于 shop_products_options 表的查询.

                But products aren't sorted. I get a query that only works with shop_products_options table.

                SELECT * FROM `shop_products_options` WHERE `shop_products_options`.`product_id` in ('8', '9', '10', '11', '12') ORDER BY `pinned` DESC
                

                如何解决?

                推荐答案

                急切加载使用单独的查询,因此您需要加入:

                Eager loading uses separate queries so you need join for this:

                $products = ShopProduct::join('shop_products_options as po', 'po.product_id', '=', 'products.id')
                   ->orderBy('po.pinned', 'desc')
                   ->select('products.*')       // just to avoid fetching anything from joined table
                   ->with('options')         // if you need options data anyway
                   ->paginate(5);
                

                SELECT 子句是为了不将连接的列附加到您的 Product 模型中.

                SELECT clause is there in order to not appending joined columns to your Product model.

                根据@alexw 评论 - 如果需要,您仍然可以包含连接表中的列.您可以将它们添加到 select 或调用 addSelect/selectRaw

                edit: as per @alexw comment - you still can include columns from joined tables if you need them. You can add them to select or call addSelect/selectRaw etc.

                这篇关于Laravel Eloquent 按关系表列排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Laravel Eloquent - 附加与同步 下一篇:如何合并两个 Eloquent 集合?

                相关文章

                最新文章

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

                    <legend id='xHrXZ'><style id='xHrXZ'><dir id='xHrXZ'><q id='xHrXZ'></q></dir></style></legend>
                    <tfoot id='xHrXZ'></tfoot>
                    • <bdo id='xHrXZ'></bdo><ul id='xHrXZ'></ul>
                  1. <small id='xHrXZ'></small><noframes id='xHrXZ'>