繁体   English   中英

如何在 Laravel 中使用 Where 条件和连接查询

[英]How to Use Where Condition With Join Query in Laravel

请建议我如何在 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)

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

提前致谢

这能解决问题吗? 当您想对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');   
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM