thinkphp一对多关联,关联预载入带查询条件,对关联数据进行条件筛选
实现方式
with中使用匿名函数 传入关联的条件
->with([‘times’ => function($query){
$query->where(‘type’,2);
}])
代码
public function times()
{
return $this->hasMany(ScheduleTime::class);
}
$res = $this
->field('*')
->with(['times' => function($query){
$query->where('type',2);
}])
->where($where)
->paginate([
'list_rows' => $size,
'page' => $page,
]);
```