直接上代码
public function index(Request $request)
{
//$word为关联表要查询的字段内容
$word=$request->get('word');
//$date为本表表要查询的字段内容
$date=$request->get('date');
//重点啊来了这里如果使用with来查询,只能过滤要查询的内容也就是说值是死的不能用活值
//查阅了资料使用wherehas可以实现动态内容过滤
//这里在doctor模型中也就是关联表中模糊查询了两个字段
//下面的when查询的是Scheduling模型也就是本表的字段
$data=Scheduling::wherehas('doctor',function ($querys) use ($word){
$querys->where('doctor_name', 'like' ,"%$word%")
->orWhere('department', 'like' ,"%$word%");
})->when(
$date,
function ($query,$date){
return $query->where([
['week','like',"%$date%",'OR'],
['last_week','like',"%$date%",'OR'],
['next_week','like',"%$date%",'OR'],
]);
},
function ($query){
return $query;
}
)->paginate(3);
return view('scheduling.index',compact('data'));
}