WhereHas 中关联模型的调用范围 Laravel 5.5
Calling Scope of Related Model in WhereHas Laravel 5.5
我正在使用 Lumen 5.5 并希望创建一个范围,将查询限制为另一个模型中的约束。这是我的相关代码。
Post 型号:
public function user()
{
return $this->belongsTo('App\User', 'user-id', 'user-id');
}
public function scopeActive()
{
return $this->where('status', '=', '1')->whereHas('user', function($q) {
$q->activeUser();
});
}
在用户模型中:
public function posts()
{
return $this->hasMany('App\Post', 'user-id');
}
public function scopeActiveUser()
{
return $this->whereDate('seen', '>=', date('Y-m-d', strtotime('-1 months')));
}
Post 模型中 whereHas
中的 activeUser()
似乎没有被触发。
用 whereDate('seen', '>=', date('Y-m-d', strtotime('-1 months')))
替换 activeUser()
工作正常但错过了 Lumen/Laravel.
的作用域能力
我在这里错过了什么?
按照说明添加 $query
部分 in the docs:
public function scopeActive($query)
{
return $query->where('status', '=', '1')->whereHas('user', function($q) {
$q->activeUser();
});
}
并且:
public function scopeActiveUser($query)
{
return $query->whereDate('seen', '>=', date('Y-m-d', strtotime('-1 months')));
}
我正在使用 Lumen 5.5 并希望创建一个范围,将查询限制为另一个模型中的约束。这是我的相关代码。
Post 型号:
public function user()
{
return $this->belongsTo('App\User', 'user-id', 'user-id');
}
public function scopeActive()
{
return $this->where('status', '=', '1')->whereHas('user', function($q) {
$q->activeUser();
});
}
在用户模型中:
public function posts()
{
return $this->hasMany('App\Post', 'user-id');
}
public function scopeActiveUser()
{
return $this->whereDate('seen', '>=', date('Y-m-d', strtotime('-1 months')));
}
Post 模型中 whereHas
中的 activeUser()
似乎没有被触发。
用 whereDate('seen', '>=', date('Y-m-d', strtotime('-1 months')))
替换 activeUser()
工作正常但错过了 Lumen/Laravel.
我在这里错过了什么?
按照说明添加 $query
部分 in the docs:
public function scopeActive($query)
{
return $query->where('status', '=', '1')->whereHas('user', function($q) {
$q->activeUser();
});
}
并且:
public function scopeActiveUser($query)
{
return $query->whereDate('seen', '>=', date('Y-m-d', strtotime('-1 months')));
}