首页 > 解决方案 > 如何在 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)

但它并没有解决我的问题所以请给我一个解决方案

提前致谢

标签: mysqllaravel

解决方案


这能解决问题吗?当您想对您的函数进行分组whereorWheres使用函数作为参数时

$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');   
} 

推荐阅读