jeudi 13 décembre 2018

Laravel Eloquent: Add filter in where (if-else for Conditional Queries)?

I have this code :

$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();

I need add filters:

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

I've seen a lot of posts regarding multiple Where in laravel but not a single one talking about those special wheres.

how to add filter in query?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire