dongliling6336 2017-04-28 09:27
浏览 712

如何在laravel中的“whereIn”中使用“if”条件

Below is my query which one i used and working fine for me.

    if($t_requested['category_id'])
    {
        $t_query_condition['p.category_id'] = $t_requested['category_id'];
    }
    if($t_requested['brand_id'])
    {
        $t_query_condition['p.brand_id'] = $t_requested['brand_id'];
    }

    $browseData = DB::table('products as p')
        ->leftjoin('categories as cp', 'p.category_id', '=', 'cp.id')
        ->leftjoin('brands as bp', 'p.brand_id', '=', 'bp.id')
        ->select('p.id as product_id','p.name as product_name','cp.title as category_name','bp.name as brand_name','p.product_weight',
               'p.short_description','p.product_price','p.special_price','p.stock_quantity')
        ->orderBy('p.created_by', "desc")
        ->offset(0)
        ->limit(10)
        ->where($t_query_condition)
        ->get();

but Now i have getting multiple id in "category_id" and "brand_id", i want to used whereIn but it used with of condition. if i get category_id or brand_id null then it's skip.

thanks in advance.

  • 写回答

2条回答 默认 最新

  • duanji8887 2017-04-28 10:29
    关注

    I think it is very simple, you have to pass array of values instead of one value

    I have tried to write options for achieve this, you have to follow which is suitable for you

    if($t_requested['category_id'])
    {
       $t_query_condition['p.category_id'] = $t_requested['category_id']; // it should be array OR
    
       $t_query_condition['p.category_id'] = explode(",",$t_requested['category_id']); // if you get value comma saperated
    
    $t_query_condition['p.category_id'] = explode(",",$t_requested['category_id']); // if you get value comma saperated
    
    $t_query_condition['p.category_id'] = [$t_requested['category_id']]; // if you want to convert one value into array
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀