Php WhereHas Laravel 5.5中相关模型的调用范围

Php WhereHas Laravel 5.5中相关模型的调用范围,php,laravel,lumen,Php,Laravel,Lumen,我正在使用Lumen 5.5,希望创建一个范围,将查询限制为另一个模型中的约束。这是我的相关代码 后模型: public function user() { return $this->belongsTo('App\User', 'user-id', 'user-id'); } public function scopeActive() { return $this->where('status', '=', '1')->whereHas('user', fun

我正在使用Lumen 5.5,希望创建一个范围,将查询限制为另一个模型中的约束。这是我的相关代码

后模型:

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()
似乎没有被触发

activeUser()
替换为
whereDate('seen','>=',date('Y-m-d',strotime('-1个月'))
效果良好,但忽略了Lumen/Laravel的范围界定功能


这里缺少什么?

添加
$query
部分,如下所述:

以及:


@Maraboc的可能副本为我指明了正确的方向:)!工作正常,我会尽快接受您的答案-同时我正在努力了解我什么时候会使用$this而不是$query?@Michał您可以在模型中定义的简单方法中使用
$this
,例如
return$this->find(4),但不能在本地范围内执行此操作。
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')));
}