I have 2 lines of code here but i want to combine all 2 lines into 1
// How to combine these 2 lines here
$product = $this->productService->searchProduct($request);
$product = Product::paginate(9);
Controller:
public function searchProduct(SearchProductRequest $request)
{
$search = $request->search;
$min_price = $request->min_price;
$max_price = $request->max_price;
$product = Product::query('products')
->where('name', 'like', "%{$search}%")
->where('price', [$min_price])
->orWhere('price',[$max_price])
->orderBy('id');
return $product;
}
CodePudding user response:
Try this
public function searchProduct(SearchProductRequest $request)
{
$products = Product::select('id', 'name', 'price')
->when($request->input('search'), function($q){
$q->where('name', 'like', '%' . $request->input('search') . '%');
})
->when($request->input('min_price'), function($q){
$q->where('price', $request->input('min_price'));
})
->when($request->input('max_price'), function($q){
$q->orWhere('price', $request->input('max_price'));
})
->orderBy('id')
->paginate(9);
return $products;
}
CodePudding user response:
you can try :
$product = $this->productService->searchProduct($request)->paginate(9);
take a look at your view blade, have you added links(); like this
<div >
{{ $product->links() }}
</div>
CodePudding user response:
$product = !empty($this->productService->searchProduct($request)?Product::paginate(9):'';