How to Use Where Condition With Join Query in Laravel

ayushflitzip :

Please suggest me how to I use multiple join queries with multiple where clause condition in laravel.

I have to disable use features from tables like:- my table name is calllog here user call records exist and another table with name disableapp so if user status is 1 data records show or if it is 0 data disable I do this using Joinquery.

My Code is

$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 here][1]][1] [![enter image description here][2]][2]

I only need if disableaap table blanks then the user data list shown in records and if their status is 0 then it hides and if 1 then it will show. User is allowed to do that but the first time it will show in records.

Once i uses

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

but it did not solve my problem soo please suggest me a solution

Thanks in advance

Malkhazi Dartsmelidze :

Does this solve problem? When you want to group your where and orWheres use function as parameter

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=23630&siteId=1