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

    <legend id='5e9wr'><style id='5e9wr'><dir id='5e9wr'><q id='5e9wr'></q></dir></style></legend>

      • <bdo id='5e9wr'></bdo><ul id='5e9wr'></ul>
      1. 如何在与 Laravel Eloquent 方法 WITH 连接的元素上使

        时间:2023-09-22

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

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

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

              <tfoot id='oUria'></tfoot>
              • <legend id='oUria'><style id='oUria'><dir id='oUria'><q id='oUria'></q></dir></style></legend>
                    <tbody id='oUria'></tbody>
                  本文介绍了如何在与 Laravel Eloquent 方法 WITH 连接的元素上使用 orderby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  问题是查询找不到specific_method(specific_method, specific_model,SpecificModel,specificMethod etc...),应该与Laravel Eloquent中的WITH方法连接.任何想法如何解决它?我的代码:

                  The problem is that the query can't find the specific_method(specific_method, specific_model,SpecificModel,specificMethod etc...), that should been joined with the method WITH from Laravel Eloquent. Any ideas how to solve it? My code:

                  //SpecificModel
                  <?php
                  namespace AppModels;
                  use IlluminateDatabaseEloquentModel;
                  
                  class SpecificModel extends Model {
                  
                      protected $guard_name = 'web';
                      protected $table = 'SpecificTable';
                      protected $guarded = ['id'];
                  
                      public function specificMethod(){
                          return $this->belongsTo('AppModelsAnotherModel','AnotherModel_id');
                      }
                  }
                  
                  
                  //AnotherModel
                  <?php
                  namespace AppModels;
                  use IlluminateDatabaseEloquentModel;
                  
                  class AnotherModel extends Model {
                  
                      protected $guard_name = 'web';
                      protected $table = 'AnotherTable';
                      protected $guarded = ['id'];
                  }
                  
                  //Query method
                  $model = app('AppModelsSpecificModel');
                  $query = $model::with('specificMethod:id,title');
                  $query = $query->orderBy('specific_method.title','desc');
                  return $query->get();
                  
                  
                  //Error
                  Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 
                  '"specific_method.title"' in 'order clause' (SQL: select * from 
                  `SpecificModel` where `SpecificModel`.`deleted_at` is null order by 
                  `specific_method`.`title` desc)
                  

                  推荐答案

                  发生这种情况是因为belongsTo 关系没有像您期望的那样执行join 查询(正如您从错误中看到的得到).它执行另一个查询以获取相关模型.因此,您将无法通过相关模型列订购原始模型.

                  This happens because the belongsTo relationship does not execute a join query as you expect it to (as you can see from the error you get). It executes another query to get the related model(s). As such you will not be able to order the original model by related models columns.

                  基本上,会发生 2 个查询:

                  Basically, 2 queries happen:

                  1. 使用 SELECT * from originalModel ...*

                  使用 SELECT * from relatedModel where in id (originalModelForeignKeys)

                  然后 Laravel 做了一些魔术,将第二个查询中的模型附加到第一个查询中的正确模型上.

                  Then Laravel does some magic and attaches the models from the 2nd query to the correct models from the first query.

                  您需要执行实际的join能够以您想要的方式订购.

                  You will need to perform an actual join to be able to order the way you want it to.

                  这篇关于如何在与 Laravel Eloquent 方法 WITH 连接的元素上使用 orderby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 Laravel eloquent 中进行子查询? 下一篇:数据仅在 Laravel 应用程序中插入最后一个数组

                  相关文章

                  最新文章

                • <tfoot id='ngSJs'></tfoot>
                • <legend id='ngSJs'><style id='ngSJs'><dir id='ngSJs'><q id='ngSJs'></q></dir></style></legend>
                    <bdo id='ngSJs'></bdo><ul id='ngSJs'></ul>

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

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