【问题标题】:How to sort global scope by attribute LARAVEL如何按属性 LARAVEL 对全局范围进行排序
【发布时间】:2020-01-15 20:21:34
【问题描述】:

我通常使用模型上的全局范围按列对记录进行排序,但我需要按属性对其进行排序。

protected static function boot()
{
  parent::boot();
  static::addGlobalScope('order', function (Builder $builder) {
      $builder->orderBy('MyAttribute', 'dsc');
  });
}

【问题讨论】:

  • 请更清楚并提供更多信息...什么属性?你的表是如何构成的?什么时候需要按 this 属性排序?
  • 属性我假设有雄辩的属性?

标签: php laravel sorting eloquent global-scope


【解决方案1】:

您可以使用withoutGlobalScope 方法从查询中删除范围:

Model::withoutGlobalScope('order')->orderBy('attribute', 'order')->get();

否则您可以删除所有全局范围:

Model::withoutGlobalScopes()->orderBy('attribute', 'order')->get();

您可以在此处阅读有关Removing Global Scopes 的更多信息:https://laravel.com/docs/6.x/eloquent

【讨论】:

    【解决方案2】:

    你的意思是像

    //...
    ->orderByRaw('(updated_at - created_at) desc')
    //...
    

    【讨论】:

      【解决方案3】:

      我认为您的属性是访问器,它不是数据库中的真实列,因此您无法通过查询构建器或雄辩构建器直接访问它而无需退出数据库。

      不过,你可以通过collection或者json来访问。因此,如果要对记录进行排序,则需要在收集时使用sortBysortByDesc。您需要从数据库中获取结果。所以你可以对它们进行排序。

      Model::all()->sortBy('MyAttribute');
      Model::all()->sortByDesc('MyAttribute');
      

      【讨论】:

        【解决方案4】:

        您可以通过三种方式做到这一点:

        • A) 您可以将其应用于所有查询。
        • B) 或者您可以在模型中创建单个范围并在查询中调用它。
        • C) 或者您可以直接在您的查询中进行排序

        A) 您可以将其应用于所有查询:

        1. 创建一个位于/app/Scopes/SortByScope.php 的新范围文件

        那么该文件 (SortByScope.php) 应该如下所示:

        <?php
        
        namespace App\Scopes;
        
        use Illuminate\Database\Eloquent\Builder;
        use Illuminate\Database\Eloquent\Model;
        use Illuminate\Database\Eloquent\Scope;
        
        class SortByScope implements Scope
        {
            /**
             * Apply the scope to a given Eloquent query builder.
             *
             * @param  \Illuminate\Database\Eloquent\Builder  $builder
             * @param  \Illuminate\Database\Eloquent\Model  $model
             * @return void
             */
            public function apply(Builder $builder, Model $model)
            {
                $builder->orderBy('MyAttribute', 'DESC');
            }
        }
        
        1. 在你的模型中包含你头脑中的调用
        <?php
        
        namespace App;
        
        use App\Scopes\SortedOrderScope;
        
        ...rest of your model...
        
        1. 在您的模型中包含以下内容:
            /**
             * Apply to all queries.
             *
             * @return void
             */
            protected static function boot()
            {
                parent::boot();
                static::addGlobalScope(new SortByScope);
            }
        
        

        https://laravel.com/docs/master/eloquent#global-scopes


        B) 或者您可以在模型中创建单个范围并在查询中调用它:

        1. 在您的模型中输入以下方法:
            /**
             * Scope a query to be sorted by MyAttribute
             *
             * @param  \Illuminate\Database\Eloquent\Builder  $query
             * @return \Illuminate\Database\Eloquent\Builder
             */
            public function scopeSortedByMyAttribute($query)
            {
                $builder->orderBy('MyAttribute', 'DESC');
            }
        
        1. 然后在您的查询中使用它:
        $results = App\MyModel::sortedByMyAttribute()->get();
        

        -或-

        $results = App\MyModel::where('foo', '=', 'bar')->sortedByMyAttribute();
        

        https://laravel.com/docs/master/eloquent#local-scopes


        C) 或者您可以直接在查询中使用以下命令进行排序:

        $results = App\MyModel::orderBy('MyAttribute', 'DESC')->get();
        

        【讨论】:

        • 我尝试使用scopeSortedByMyAttribute($query),但出现错误method illuminate database eloquent collection doesn't exist
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-14
        • 2020-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多