Laravel 默认订单由在拉拉维尔5.2之前拉拉维尔 5.2 及更高版本

2022-08-30 09:48:47

是否有一种干净的方法可以使某些模型默认按属性排序?它可以通过扩展laravel的QueryBuilder来工作,但要做到这一点,你必须重新连接它的一些核心功能 - 这是一种不好的做法。

原因

这样做的要点是 - 我的一个模型被许多其他模型大量重用,现在你必须一遍又一遍地诉诸顺序。即使为此使用闭包 - 您仍然必须调用它。能够应用默认排序会好得多,因此使用此模型且不提供自定义排序选项的每个人都会收到按默认选项排序的记录。在这里,使用存储库不是一个选项,因为它会被预先加载。

溶液

扩展基本模型:

protected $orderBy;
protected $orderDirection = 'ASC';

public function scopeOrdered($query)
{
    if ($this->orderBy)
    {
        return $query->orderBy($this->orderBy, $this->orderDirection);
    }

    return $query;
}

public function scopeGetOrdered($query)
{
    return $this->scopeOrdered($query)->get();
}

在您的模型中:

protected $orderBy = 'property';
protected $orderDirection = 'DESC';

// ordering eager loaded relation
public function anotherModel()
{
    return $this->belongsToMany('SomeModel', 'some_table')->ordered();
}

在控制器中:

MyModel::with('anotherModel')->getOrdered();
// or
MyModel::with('anotherModel')->ordered()->first();

答案 1

在拉拉维尔5.2之前

如今,我们也可以通过Laravel 4.2中引入的全局范围来解决这个问题(如果我错了,请纠正我)。我们可以定义一个范围类,如下所示:

<?php namespace App;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ScopeInterface;

class OrderScope implements ScopeInterface {

    private $column;

    private $direction;

    public function __construct($column, $direction = 'asc')
    {
        $this->column = $column;
        $this->direction = $direction;
    }

    public function apply(Builder $builder, Model $model)
    {
        $builder->orderBy($this->column, $this->direction);

        // optional macro to undo the global scope
        $builder->macro('unordered', function (Builder $builder) {
            $this->remove($builder, $builder->getModel());
            return $builder;
        });
    }

    public function remove(Builder $builder, Model $model)
    {
        $query = $builder->getQuery();
        $query->orders = collect($query->orders)->reject(function ($order) {
            return $order['column'] == $this->column && $order['direction'] == $this->direction;
        })->values()->all();
        if (count($query->orders) == 0) {
            $query->orders = null;
        }
    }
}

然后,在模型中,可以在方法中添加作用域:boot()

protected static function boot() {
    parent::boot();
    static::addGlobalScope(new OrderScope('date', 'desc'));
}

现在,模型默认排序。请注意,如果还在查询中手动定义顺序:,则它只会将其添加为辅助排序(当第一个排序的值相同时使用),并且不会覆盖。为了能够手动使用其他排序,我添加了一个(可选)宏(见上文),然后您可以执行:.MyModel::orderBy('some_column')MyModel::unordered()->orderBy('some_column')->get()

拉拉维尔 5.2 及更高版本

Laravel 5.2 引入了一种更简洁的全局范围工作方式。现在,我们唯一需要写的就是以下内容:

<?php namespace App;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class OrderScope implements Scope
{

    private $column;

    private $direction;

    public function __construct($column, $direction = 'asc')
    {
        $this->column = $column;
        $this->direction = $direction;
    }

    public function apply(Builder $builder, Model $model)
    {
        $builder->orderBy($this->column, $this->direction);
    }
}

然后,在模型中,可以在方法中添加作用域:boot()

protected static function boot() {
    parent::boot();
    static::addGlobalScope(new OrderScope('date', 'desc'));
}

要删除全局范围,只需使用:

MyModel::withoutGlobalScope(OrderScope::class)->get();

无需额外作用域类的解决方案

如果您不喜欢为范围使用整个类,则可以(从Laravel 5.2开始)在模型的方法中以内联方式定义全局范围:boot()

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

您可以使用以下命令删除此全局作用域:

MyModel::withoutGlobalScope('order')->get();

答案 2

在Laravel 5.7中,您现在可以简单地在模型的引导功能中使用:addGlobalScope

use Illuminate\Database\Eloquent\Builder;

protected static function boot()
{
    parent::boot();

    static::addGlobalScope('order', function (Builder $builder) {
        $builder->orderBy('created_at', 'desc');
    });
}

在上面的示例中,我对模型进行排序,以便首先获取最新的记录。您可以更改它以满足您的需求。created_at desc


推荐