mysql - 如何在 Laravel 中使用 where 条件和 join 查询
问题描述
请建议我如何在 laravel 中使用具有多个 where 子句条件的多个连接查询。
我必须禁用表中的使用功能,例如:-我的表名是calllog,这里存在用户调用记录,另一个名为disableapp的表,因此如果用户状态为 1 条数据记录显示或为 0 条数据禁用,我使用 Joinquery 执行此操作。
我的代码是
$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');
}
[![在此处输入图像描述][1]][1] [![在此处输入图像描述][2]][2]
我只需要如果 disableaap 表为空,则记录中显示的用户数据列表,如果它们的状态为 0,则隐藏,如果为 1,则显示。用户可以这样做,但第一次会显示在记录中。
一旦我使用
orwhere('disableapp.status', '=', Null)
但它并没有解决我的问题所以请给我一个解决方案
提前致谢
解决方案
这能解决问题吗?当您想对您的函数进行分组where
并orWheres
使用函数作为参数时
$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');
}
推荐阅读
- python - Adding header/column to my .csv file
- javascript - 如何获得特定的在 android webview 中使用 js 的 id 数据?
- python-3.x - 使用 Imaplib 在 python 中访问电子邮件内容的不同方法是什么?
- r - 如何在 stringr 中的两个模式或 r 中的 stringi 之间应用 string_extract
- spring-boot - Spring Boot ConfigurationProperties 问题
- flask - Flask:从数据库内容动态填充的表单
- linux - 用另一个列表中的下一行替换列表中的字符串
- python - Enumerating recursively with pass by reference in Python
- bash - 如何编写一个迭代创建新脚本的 bash 脚本
- php - R中的9级压缩