Method orderBy does not exist in Laravel Eloquent?

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

    If you want to get the list of all data and grab it in descending order try this:

    $post = Post::orderBy('id', 'DESC')->get();
    
    0 讨论(0)
  • 2020-12-29 05:35

    Your query is wrong.

    remove all from $products = Product::all() and then put get() at the end of your query.

    0 讨论(0)
  • 2020-12-29 05:41

    use sortByDesc('id') or simple sortBy() inside use the variable through which you wanna sort like i add id

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-29 05:46
    $table_Data = DB::table('tbl_product')->orderBy('id','DESC');
    

    You can use this...

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题