Reputation: 63
I have a piece of code like this:
public function products()
{
$product = DB::table('products')->where('is_active', NULL)->get();
if($price = request('entityprice')){
$product->orderBy('entityprice', $price);
}
// $products = DB::table('products')->where('is_active', NULL)->find($id);
// dd($product);
return view('products', compact('product'));
}
I got the following error:
Method Illuminate\Support\Collection::orderBy does not exist.
I have front page
<a href="?entityprice=desc">Sort by price descending</a>
<a href="?entityprice=asc">Sort by price</a>
Please help me)))
Upvotes: 1
Views: 5268
Reputation: 1011
Actually orderBy
is not defined for Collection
, you have to use sortBy
.
A better approach is to use orderBy
with your query. and yes, you have an if statement. So instead of if
statement, replace it with when
method which will organize your query and make it a little better and more readable:
public function products()
{
$product = DB::table('products')
->where('is_active', NULL)
->when(request('entityprice', false),
function($query, $entityprice){
return $query->orderBy('entityprice', $entityprice);
})
->get()
return view('products', compact('product'));
}
Upvotes: 2
Reputation: 1222
You couldn't use orderBy
with a collection. You should use orderBy
before get()
or use sortBy
with this collection.
Modify it:
public function products()
{
$product = DB::table('products')->where('is_active', NULL);
if($price = request('entityprice')) {
$product->orderBy('entityprice', $price);
}
return view('products', ['product' => $product->get()]);
}
or
public function products()
{
$product = DB::table('products')->where('is_active', NULL)->get();
if($price = request('entityprice')) {
$product->sortBy('entityprice', $price);
}
return view('products', ['product' => $product->get()]);
}
The first solution will order in the query statement. And the last solution will order in the collection.
Upvotes: 0
Reputation:
You should try this:
public function products()
{
$product = DB::table('products')->where('is_active', NULL)->get();
if($price = request('entityprice')){
$product->orderBy('entityprice', $price);
}
$products = $products->get();
// $products = DB::table('products')->where('is_active', NULL)->find($id);
// dd($product);
return view('products', compact('product'));
}
Upvotes: 0
Reputation: 15145
try this one
public function products()
{
$product = DB::table('products')->where('is_active', NULL);
if($price = request('entityprice')){
$product->orderBy('entityprice', $price);
}
return view('products', ['product' => $product->get()]);
}
Upvotes: 1
Reputation: 1574
You have a logical error in code
public function products()
{
$product = DB::table('products')->where('is_active', NULL);
if($price = request('entityprice')){
$product->orderBy('entityprice', $price);
}
$prod = $product->get();
// $products = DB::table('products')->where('is_active', NULL)->find($id);
// dd($product);
return view('products', compact('prod'));
}
Upvotes: 0
Reputation: 3065
try this code,
public function products()
{
$product = DB::table('products')->where('is_active', NULL);
if($price = request('entityprice')){
$product->orderBy('entityprice', $price)->get();
}
// $products = DB::table('products')->where('is_active', NULL)->find($id);
// dd($product);
return view('products', compact('product'));
}
Upvotes: 0