YouTube Icon

Code Playground.

How to make Condition based query in Laravel 5

CFG

How to make Condition based query in Laravel 5

So you use the query builder from laravel to get all the orders that belong to a particular result using condition, but you want to allow the user to filter the results.

You can now use your controller's model.

use App\DiamondMaster;

Laravel query builder in if statement.

$getrecord = DiamondMaster::where('is_delete','0')
->where('C_Weight','>=',$request->min_carat)
->where('C_Weight','<=',$request->max_carat)
->where('C_DefthP','>=',$request->min_depth)
->where('C_DefthP','<=',$request->max_depth)
->where('C_NetD','>=',$request->min_price)
->where('C_NetD','<=',$request->max_price);	
if(!empty($request->shape))
{	
	$getrecord = $getrecord->whereIn('C_Shape',explode(",", $request->shape));
}
if(!empty($request->color))
{
	$getrecord = $getrecord->whereIn('C_Color',explode(",", $request->color));
}
if(!empty($request->clarity))
{
	$getrecord = $getrecord->whereIn('C_Clarity',explode(",", $request->clarity));
}
if(!empty($request->cut))
{
	$getrecord = $getrecord->whereIn('C_Cut',explode(",", $request->cut));
}
if(!empty($request->polish))
{
	$getrecord = $getrecord->whereIn('C_Polish',explode(",", $request->polish));
}
if(!empty($request->symmetry))
{
	$getrecord = $getrecord->whereIn('C_Symmetry',explode(",", $request->symmetry));
}
if(!empty($request->flourescence))
{
	$getrecord = $getrecord->whereIn('C_Fluorescence',explode(",", $request->flourescence));
}
if(!empty($request->flourescence))
{
	$getrecord = $getrecord->whereIn('C_Fluorescence',explode(",", $request->flourescence));
}
$getrecord = $getrecord->get();




CFG