1. <small id='mIxqQ'></small><noframes id='mIxqQ'>

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

      Eloquent 中的 MySQL 按字段排序

      时间:2023-09-24

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

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

                  <tbody id='vtqZD'></tbody>
                本文介绍了Eloquent 中的 MySQL 按字段排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                当我想在 MySQL 查询中定义自定义排序顺序时,我可以这样做:

                When I want to define a custom sort order in a MySQL query I can do something like this:

                ORDER BY FIELD(language,'USD','EUR','JPN')
                

                Eloquent ORM 版本是什么?

                What would be the Eloquent ORM version of that?

                更新:

                这是解决方案,它也适用于在各个领域进行排序:

                This is the solution and it also works when ordering on various fields:

                $events = Event::with( 'type', 'location' )
                               ->orderBy( 'event_type_id' )
                               ->orderByRaw( "FIELD(status, 'good', 'bad', 'hidden', 'active', 'cancelled')" )
                               ->orderBy( 'date' );
                

                推荐答案

                直接使用 DB::raw()orderByRaw 应该可以:

                Using either DB::raw() or orderByRaw directly should work:

                $models = Model::orderByRaw('FIELD(language, "USD", "EUR", "JPN")')->get();
                // or
                $models = Model::orderBy(DB::raw('FIELD(language, "USD", "EUR", "JPN")'))->get();
                

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

                上一篇:Laravel Eloquent 如何在操作符之间使用 下一篇:如何在laravel中为每个用户生成唯一的随机值并将

                相关文章

                最新文章

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

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

                  <legend id='HkITN'><style id='HkITN'><dir id='HkITN'><q id='HkITN'></q></dir></style></legend>

                  1. <tfoot id='HkITN'></tfoot>
                      <bdo id='HkITN'></bdo><ul id='HkITN'></ul>