我需要获得所有 appeals
,appeal_stage.expiration_date
小于 NOW()
。
现在我有以下解决方案:
public function scopeExpired($query) {
$query->join('appeal_stage', 'appeals.id', 'appeal_stage.appeal_id')
->where('appeal_stage.expiration_date', '<=', new Expression('NOW()'));
}
所以,我想问一下 - 有没有更方便的方法来执行这个请求?
我的建议是以某种方式使用 Illuminate\Database\Eloquent\Relations\Pivot
,但我不太明白,这里如何使用 Pivot。
UPD 1
模型有下一个关系:
public function stages()
{
return $this->belongsToMany(Stage::class)->withPivot('prolongated_count', 'expiration_date')->withTimestamps();
}
public function appeals() {
return $this->belongsToMany(Appeal::class);
}
最佳答案
你应该能够做这样的事情:
$appeal->stages()->wherePivot('expiration_date', '<', $now)->get()
关于php - Laravel 获取具有关系枢轴条件的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46408624/