'How to combine lines of code into one line?
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;
}
Solution 1:[1]
you can try :
$product = $this->productService->searchProduct($request)->paginate(9);
take a look at your view blade, have you added links(); like this
<div class="float-left mr-3 mb-1">
{{ $product->links() }}
</div>
Solution 2:[2]
$product = !empty($this->productService->searchProduct($request)?Product::paginate(9):'';
Solution 3:[3]
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;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Babo |
| Solution 2 | Jyoti Bhandari |
| Solution 3 |
