Mysql 如何在Laravel中使用Where条件和连接查询
请建议我如何在laravel中使用带有多where子句条件的多连接查询 我必须禁用表中的使用功能,例如:-我的表名为calllog,这里存在用户调用记录,另一个表名为disableapp,因此如果用户状态为1,则显示数据记录,如果为0,则使用Joinquery执行此操作 我的代码是Mysql 如何在Laravel中使用Where条件和连接查询,mysql,laravel,Mysql,Laravel,请建议我如何在laravel中使用带有多where子句条件的多连接查询 我必须禁用表中的使用功能,例如:-我的表名为calllog,这里存在用户调用记录,另一个表名为disableapp,因此如果用户状态为1,则显示数据记录,如果为0,则使用Joinquery执行此操作 我的代码是 $callrecordings = DB::table('callrecordings') ->join('users', 'users.id', '=', 'callrecordings.user_i
$callrecordings = DB::table('callrecordings')
->join('users', 'users.id', '=', 'callrecordings.user_id')
->leftJoin('disableapp', 'callrecordings.user_id', '=', 'disableapp.user_id')
->select('callrecordings.*', 'users.expiry_date')
->where('callrecordings.user_id', '=', $user_id)
->where('disableapp.status', '=', 1)
->get();
if($callrecordings[0]->expiry_date <= Carbon::now()->toDateTimeString()){
return response()->json(['status'=>'Package Expired Sorry!', 'data'=>'']);
} else{
return $this->sendResponse($callrecordings->toArray(), 'Call Recordings retrieved successfully');
}
但这并没有解决我的问题,所以请给我一个解决方案
提前感谢这能解决问题吗?当您想将
where
和或where
分组时,请使用函数作为参数
$callrecordings = DB::table('callrecordings')
->join('users', 'users.id', '=', 'callrecordings.user_id')
->leftJoin('disableapp', 'callrecordings.user_id', '=', 'disableapp.user_id')
->select('callrecordings.*', 'users.expiry_date')
->where('callrecordings.user_id', '=', $user_id)
->where(function($q){
$q->where('disableapp.status', '=', 1)
->orWhereNull('disableapp.status');
})
->get();
if($callrecordings[0]->expiry_date <= Carbon::now()->toDateTimeString()){
return response()->json(['status'=>'Package Expired Sorry!', 'data'=>'']);
} else{
return $this->sendResponse($callrecordings->toArray(), 'Call Recordings retrieved successfully');
}
$callrecordings=DB::table('callrecordings')
->join('users','users.id','=','callrecordings.user_id'))
->leftJoin('disableapp','callrecordings.user_id','=','disableapp.user_id'))
->选择('callrecordings.*','users.expiration\u date')
->其中('callrecordings.user\u id','=',$user\u id)
->其中(函数($q){
$q->where('disableapp.status','=',1)
->或wherenull('disableapp.status');
})
->get();
if($callrecordings[0]->Expiration\u date toDateTimeString()){
return response()->json(['status'=>'Package Expired Sorry!','data'=>'');
}否则{
返回$this->sendResponse($callrecordings->toArray(),'callrecordings retrieved successfully');
}
这能解决问题吗?当您想将where
和或where
分组时,请使用函数作为参数
$callrecordings = DB::table('callrecordings')
->join('users', 'users.id', '=', 'callrecordings.user_id')
->leftJoin('disableapp', 'callrecordings.user_id', '=', 'disableapp.user_id')
->select('callrecordings.*', 'users.expiry_date')
->where('callrecordings.user_id', '=', $user_id)
->where(function($q){
$q->where('disableapp.status', '=', 1)
->orWhereNull('disableapp.status');
})
->get();
if($callrecordings[0]->expiry_date <= Carbon::now()->toDateTimeString()){
return response()->json(['status'=>'Package Expired Sorry!', 'data'=>'']);
} else{
return $this->sendResponse($callrecordings->toArray(), 'Call Recordings retrieved successfully');
}
$callrecordings=DB::table('callrecordings')
->join('users','users.id','=','callrecordings.user_id'))
->leftJoin('disableapp','callrecordings.user_id','=','disableapp.user_id'))
->选择('callrecordings.*','users.expiration\u date')
->其中('callrecordings.user\u id','=',$user\u id)
->其中(函数($q){
$q->where('disableapp.status','=',1)
->或wherenull('disableapp.status');
})
->get();
if($callrecordings[0]->Expiration\u date toDateTimeString()){
return response()->json(['status'=>'Package Expired Sorry!','data'=>'');
}否则{
返回$this->sendResponse($callrecordings->toArray(),'callrecordings retrieved successfully');
}
->where('disableapp.status','=',1)
-此条件必须是LeftLoin条件的一部分,而不是where的一部分。类似于->leftJoin('disableapp',Raw::'callrecordings.user_id=disableapp.user_id AND disableapp.status=1')
..->where('disableapp.status','=','1)
-此条件必须是LeftLoin条件的一部分,而不是where的一部分。类似于->leftJoin('disableapp',Raw:'callrecordings.user\u id=disableapp.user\u id AND disableapp.status=1')
…感谢您的回答,它正在工作,但当状态为0时,它会显示此消息“Undefined offset:0”为什么?????如果条件为$callrecordings[0]
,则会引发此错误。首先检查是否收到任何结果,然后使用->first()
方法获取第一个结果如果($callrecordings->first()&&&$callrecordings->first()->expiration\u date再次在数据库表中执行状态0时,它会给出相同的错误消息感谢您的回答,它正在工作,但当状态为0时,它会给出此消息“未定义的偏移量:0”为什么?如果条件为($callrecordings->first()&&$callrecordings->first()),则会引发此错误。首先检查是否收到任何结果,然后使用->first()
方法获取第一个结果如果($callrecordings->first()&$callrecordings->first())->expiration\u date当我在数据库表中执行状态0时,它再次给出相同的错误消息