在拉拉维尔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();