如何在Laravel中使用带有连接查询的where条件

问题描述 投票:1回答:1

请向我建议如何在laravel中将多个join查询与多个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');   
} 

enter image description hereenter image description here

我只需要在disableaap表为空白时在记录中显示用户数据列表,如果它们的状态为0,则将其隐藏,如果为1,则将显示它。允许用户执行此操作,但是第一次将它显示在记录中。

我使用过一次

orwhere('disableapp.status', '=', Null)

但是它不能解决我的问题,所以请建议我一个解决方案

提前感谢

mysql laravel
1个回答
0
投票

这可以解决问题吗?当您想对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');   
} 
© www.soinside.com 2019 - 2024. All rights reserved.