我有一个具有SoftDelete功能的帖子模型和一个active bool 字段来确定帖子事件状态。
class Post extends Model implements SluggableInterface
{
use SoftDeletes;
protected $primaryKey = 'post_id';
.
.
.
}
此外,帖子模型还有一个 start_date
字段,用于保存发布帖子的开始日期。
现在,我想使用Anonymous GlobalScope Laravel 5.2除了非软删除模型之外,仅过滤和获取事件帖子以及其 start_dat
e 为 NULL 或小于 Now() 的帖子。
为此,我将其添加到 Post
模型中:
protected static function boot()
{
parent::boot();
static::addGlobalScope('active', function(Builder $builder){
$builder->where('active',1);
});
static::addGlobalScope('scheduled', function(Builder $builder){
$builder
->whereNull('start_date')->orWhere(function ($query) {
$query->where('start_date', '<=', Carbon::now());
});
});
}
active
单独的全局范围工作正常,但是当我添加名为 scheduled
的第二个范围时,返回所有记录,包括软删除和 inActive 模型。
什么是问题?我无法理解
最佳答案
这是因为您正在使用orWhere
。在这些情况下,使用 Laravel 调试栏查看原始 SQL 非常有帮助,因为我敢打赌,您的 select 语句如下所示:
SELECT * FROM table WHERE deleted_at IS NULL AND active=1 AND state_date IS NULL OR (start_Date <= now())
这将选择满足一个或条件的任何内容。
要解决这个问题,您应该创建 scheduled
看起来像这样。
static::addGlobalScope('scheduled', function(Builder $builder) {
$builder->where(function($query)) {
$query->whereNull('start_date');
$query->orWhere('start_date', '<=', Carbon::now());
});
});
这(希望)会让您的查询看起来像这样:
SELECT * FROM table WHERE deleted_at IS NULL AND active=1 AND (state_date IS NULL OR start_Date <= now())
这就是我认为你想要的。
关于php - 在 Laravel 中为模型定义多个全局范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36967286/