I am stuck on one solution I want to get column as per user in put value.
for 1000 Rs. i want all data below 1000.
for 2000 Rs. I want data between 1000 & 2000.
for 3000 Rs. I want data between 2000 & 3000.
I have done it like below but it is not working.
$product = Product::select('product_price')
->get();
foreach ($product as $value) {
if($value->product_price == 1000){
$prod1 = Product::join('company', 'timeline_product.company_id', '=', 'company.company_id')
->select('company.name', 'company.logo', 'timeline_product.company_id', 'timeline_product.product_name', 'timeline_product.product_desc', 'timeline_product.product_price','timeline_product.reward_points' ,'timeline_product.created_at', 'timeline_product.updated_at')
->where('timeline_product.product_price','<=',1000)
->orderBy('timeline_product.updated_at', 'desc')
->limit($limit)
->offset($offset)
->get();
// $post = array('result' => $prod1);
}elseif ($value->product_price == 2000) {
$prod2 = Product::join('company', 'timeline_product.company_id', '=', 'company.company_id')
->select('company.name', 'company.logo', 'timeline_product.company_id', 'timeline_product.product_name', 'timeline_product.product_desc', 'timeline_product.product_price','timeline_product.reward_points' ,'timeline_product.created_at', 'timeline_product.updated_at')
->whereBetween('timeline_product.product_price', [1000, 2000])
->orderBy('timeline_product.updated_at', 'desc')
->limit($limit)
->offset($offset)
->get();
// $post = array('result' => $prod2);
}elseif ($value->product_price == 3000) {
$prod3 = Product::join('company', 'timeline_product.company_id', '=', 'company.company_id')
->select('company.name', 'company.logo', 'timeline_product.company_id', 'timeline_product.product_name', 'timeline_product.product_desc', 'timeline_product.product_price','timeline_product.reward_points' ,'timeline_product.created_at', 'timeline_product.updated_at')
->whereBetween('timeline_product.product_price', [2000, 3000])
->orderBy('timeline_product.updated_at', 'desc')
->limit($limit)
->offset($offset)
->get();
// $post = array('result' => $prod3);
}elseif ($value->product_price == 4000) {
$prod4 = Product::join('company', 'timeline_product.company_id', '=', 'company.company_id')
->select('company.name', 'company.logo', 'timeline_product.company_id', 'timeline_product.product_name', 'timeline_product.product_desc', 'timeline_product.product_price','timeline_product.reward_points' ,'timeline_product.created_at', 'timeline_product.updated_at')
->whereBetween('timeline_product.product_price', [3000, 4000])
->orderBy('timeline_product.updated_at', 'desc')
->limit($limit)
->offset($offset)
->get();
// $post = array('result' => $prod4);
}
// array_push($product,$post);
}
Please help to achieve the same. Thanks,
I guess this what you need:
Product::join('company', 'timeline_product.company_id', '=', 'company.company_id')
->select('company.name', 'company.logo', 'timeline_product.company_id', 'timeline_product.product_name', 'timeline_product.product_desc', 'timeline_product.product_price', 'timeline_product.reward_points', 'timeline_product.created_at', 'timeline_product.updated_at')
->when(request('product_price') <= 1000, function ($q) {
$q->where('timeline_product.product_price', '<=', 1000);
})
->when(request('product_price') > 1000 && request('product_price') <= 2000, function ($q) {
$q->whereBetween('timeline_product.product_price', [1000, 2000]);
})
->when(request('product_price') > 2000 && request('product_price') <= 3000, function ($q) {
$q->whereBetween('timeline_product.product_price', [2000, 3000]);
})
->when(request('product_price') > 3000 && request('product_price') <= 4000, function ($q) {
$q->whereBetween('timeline_product.product_price', [3000, 4000]);
})
->orderBy('timeline_product.updated_at', 'desc')
->limit($limit)
->offset($offset)
->get();