Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort records in alphabetical order in Laravel

Tags:

php

laravel

blade

How to sort records in alphabetical order in laravel?

public function index()
{
    $comproducts = Comproduct::paginate(3);

    $items = Item::orderBy('name')->all();        

    return view('computer', compact(['comproducts', 'items']));

}

This is not working correctly. This shows

Call to undefined method Illuminate\Database\Query\Builder::all()

this error. How can i fix this?

like image 484
K.Mihiranga Avatar asked Nov 12 '17 08:11

K.Mihiranga


2 Answers

I use get() instead , you can't modify query with method all() and also it is static function

  $items = Item::orderBy('name')->get(); 
like image 196
Niklesh Raut Avatar answered Oct 22 '22 01:10

Niklesh Raut


you can do any of the following depending in the order that you wish to sort

this for ascending order

 $students = Student::whereId($id)->orderBy('name')->get()->all();

this for Descending order

 $students = Student::whereId($id)->orderByDesc('name')->get()->all();
like image 31
Justice Selorm Bruce Avatar answered Oct 22 '22 03:10

Justice Selorm Bruce