Method orderBy does not exist in Laravel Eloquent?

前端 未结 7 1970
执念已碎
执念已碎 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:47

    You are first getting all() data and then trying to sort which is wrong. You have to fix this by removing

    $products = Product::all()
    

    and changing your code into something like this

    if ($search_value) {
        $products = Product::where('name', 'LIKE', "%$search_value%");
    }
    else {
        $products = Product::orderBy('created_at', 'desc')->skip(10)->take(10)->with('tags')->get();
    }
    

    Hope you get idea to tweak your code.

提交回复
热议问题