Laravel Eloquent:在哪里添加过滤器(如果条件查询为if-else)?

时间:2018-12-13 09:27:57

标签: laravel laravel-5 eloquent

我有此代码:

Wordpress 5.0.1

我需要添加过滤器:

$result = DB::table('product as p')
      ->join('master as cl', 'p.color', '=', 'cl.name')
      ->join('master as brand', 'p.brand', '=', 'brand.name')
      ->where('p.brand', $brandname)
      ->whereRaw("p.del_flag = 0  and cl.is_active = 1 and brand.is_active = 1 and p.product_type = 1")
      ->select('p.name')
      ->groupBy('p.name')
      ->orderBy('p.name')
      ->take(3)
      ->get();

我看过很多关于laravel中多个位置的帖子,但没有一个谈论那些特殊的位置。

如何在查询中添加过滤器?

4 个答案:

答案 0 :(得分:2)

首先将查询传递给变量,然后更新查询变量。

$query = DB::table('product as p')
      ->join('master as cl', 'p.color', '=', 'cl.name')
      ->join('master as brand', 'p.brand', '=', 'brand.name')
      ->where('p.brand', $brandname)
      ->whereRaw("p.del_flag = 0  and cl.is_active = 1 and brand.is_active = 1 and p.product_type = 1")
      ->select('p.name')
      ->groupBy('p.name')
      ->orderBy('p.name')
      ->take(3);

if( isset($_POST['brand'])) {
    $query->where('p.brand', $_POST['brand'])
}
if (isset($_POST['color'])) {
    $query->where('p.color', $_POST['color'])
}
if (isset($_POST['product_type'])) {
    $query->where('p.product_type', $_POST['product_type'])
}

$result = $query->get();

更新
正如@Amol Rokade所述,您还可以使用when()函数:

$result = DB::table('product as p')
      ->join('master as cl', 'p.color', '=', 'cl.name')
      ->join('master as brand', 'p.brand', '=', 'brand.name')
      ->where('p.brand', $brandname)
      ->whereRaw("p.del_flag = 0  and cl.is_active = 1 and brand.is_active = 1 and p.product_type = 1")
      ->when(isset($_POST['brand']), function ($query) {
            $query->where('p.brand', $_POST['brand']);
      })
      ->when(isset($_POST['color']), function ($query) {
            $query->where('p.color', $_POST['color']);
      })
      ->when(isset($_POST['product_type']), function ($query) {
            $query->where('p.product_type', $_POST['product_type']);
      })
      ->select('p.name')
      ->groupBy('p.name')
      ->orderBy('p.name')
      ->take(3)
      ->get();

答案 1 :(得分:0)

如果您想在查询中添加过滤器,则可以通过创建类似查询对象的方法来实现

$result = DB::table('product as p')
      ->join('master as cl', 'p.color', '=', 'cl.name')
      ->join('master as brand', 'p.brand', '=', 'brand.name')
      ->where('p.brand', $brandname)
      ->whereRaw("p.del_flag = 0  and cl.is_active = 1 and brand.is_active = 1 and p.product_type = 1")
      ->select('p.name')
      ->groupBy('p.name')
      ->orderBy('p.name');


if(isset($_POST['brand'])) {
    $result->where('p.brand', $_POST['brand'])
}
if(isset($_POST['color'])) {
    $result->where('p.color', $_POST['color'])
}
if(isset($_POST['product_type'])) {
    $result->where('p.product_type', $_POST['product_type'])
}

$result->take(3)->get();

可以解决问题。

答案 2 :(得分:0)

尝试

$query = DB::table('product as p')
  ->join('master as cl', 'p.color', '=', 'cl.name')
  ->join('master as brand', 'p.brand', '=', 'brand.name')
  ->where(function($query) use ($brandname,$_POST){
      $query->where('p.brand', $brandname);
      $query->whereRaw("p.del_flag = 0  and cl.is_active = 1 and brand.is_active = 1 and p.product_type = 1");

      if( isset($_POST['brand'])) {
         $query->where('p.brand', $_POST['brand']);
      }
      if (isset($_POST['color'])) {
         $query->where('p.color', $_POST['color']);
      }
      if (isset($_POST['product_type'])) {
         $query->where('p.product_type', $_POST['product_type']);
      }
  })
  ->select('p.name')
  ->groupBy('p.name')
  ->orderBy('p.name')
  ->take(3);
  ->get();

希望这对您有帮助

答案 3 :(得分:0)

我的最后一个代码:

$querydata = DB::table('product as p')
  ->join('master as cl', 'p.color', '=', 'cl.name')
  ->join('master as brand', 'p.brand', '=', 'brand.name')
  ->where('p.brand', $brandname)
  ->whereRaw("p.del_flag = 0  and cl.is_active = 1 and brand.is_active = 1 and p.product_type = 1")
  ->select('p.name')
  ->groupBy('p.name')
  ->orderBy('p.name')
  ->take(3);



  if(!empty($_POST)) {
      unset($_POST['_token']);

  foreach ($_POST as $column => $data) {
    if(empty($data)) {
      continue;
    }

    if(is_array($data)) {

      $querydata = $querydata->where(function($query) use ($column, $data) {
          foreach ($data as $value) {
            $query->orWhere('p.'.$column, $value);
          }
      });
    } else {
      $querydata->where('p.'.$column, $data);
    }

  }


 }

  $result = $querydata->get();

我希望能帮助别人。