查询设计到两个表两个字段使用
$list=ChargeOrderModel::query()
->orwhere('car_no','like',"%{$search}%")
->orwhereHas('user', function ($query) use ($search) {
$query->where('realname', 'like', '%'.$search.'%');
})
->with(["user:id,realname"])
//->where('type',$pype)
//->orderBy('id','desc')
->get()
//->toArray();
print_r(json_encode($list));
查询结果便是,车牌号或者名字的模糊查询,
详细查看官方文档的,whereHas
Model 方法需要添加 user 关联
public function user(){
return $this->belongsTo('App\Models\CloUser','user_id','id');
}