Method orderBy does not exist in Laravel Eloquent?

前端 未结 7 1966
执念已碎
执念已碎 2020-12-29 05:16

I have a piece of code like this:

$products = Product::all()

if ($search_value) {
    $products = $products->where(\'name\', \'LIKE\', \"%$search_value%\         


        
7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-29 05:42

    You're trying to use orderBy() method on Eloquent collection. Try to use sortByDesc() instead.

    Alternatively, you could change $products = Product::all(); to $products = new Product();. Then all your code will work as you expect.

提交回复
热议问题