Reputation: 866
some time some variables are null so how check the variable is not null and apply direct in query
$fiter_products = DB::table('products')->DISTINCT('modalid')->DISTINCT('brandid')->select('rimdiameter','modalid','modalname1','modalname2','image1','brand','minprice','maxprice')->where('hubbore','>=',$centre_bore)->where('boltpattern',$boltptn)->where('rimdiameter', $diameter)->where('rimwidth', $width)->where('rimwidthfront', $frontwid)->where('construction', $construct)->where('modalname2', $color)->where('brand', $brand)->get();
any way to solve this issue ?
Upvotes: 0
Views: 50
Reputation: 13404
You may pass another Closure as the third parameter to the when method. This Closure will execute if the first parameter evaluates as false.
$fiter_products = DB::table('products')
->DISTINCT('modalid')
->DISTINCT('brandid')
->select('rimdiameter','modalid','modalname1','modalname2','image1','brand','minprice','maxprice')
->when($centre_bore, function($query, $centre_bore) {
$query->where('hubbore','>=',$centre_bore);
})->when($boltptn, function($query, $boltptn) {
$query->where('boltpattern',$boltptn);
})...
Upvotes: 2
Reputation: 159
$product = DB::table('products')->DISTINCT('modalid')->DISTINCT('brandid')->select('rimdiameter','modalid','modalname1','modalname2','image1','brand','minprice','maxprice');
if(isset($centre_bore)){
$product->where('hubbore','>=',$centre_bore);
}
if(isset($boltptn)){
$product->where('boltpattern',$boltptn);
}
if(isset($diameter)){
$product->where('rimdiameter', $diameter);
}
if(isset($width)){
$product->where('rimwidth', $width);
}
if(isset($frontwid)){
$product->where('rimwidthfront', $frontwid);
}
if(isset($construct)){
$product->where('construction', $construct);
}
if(isset($color)){
$product->where('modalname2', $color);
}
if(isset($brand)){
$product->where('brand', $brand);
}
$fiter_products = $product->get();
Upvotes: 0
Reputation: 506
You can use the whereNotNull('field)
method. From the docs:
The whereNotNull method verifies that the column's value is not NULL
Upvotes: 0