How to sort records in alphabetical order in Laravel

后端 未结 4 777
温柔的废话
温柔的废话 2020-12-17 19:17

How to sort records in alphabetical order in laravel?

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

    $items = Item::orderBy(\'nam         


        
相关标签:
4条回答
  • 2020-12-17 19:53

    That's how you sort it, orderBy() comes after all():

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

    Reference: https://laravel.com/docs/5.5/collections

    0 讨论(0)
  • 2020-12-17 19:55

    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();
    
    0 讨论(0)
  • 2020-12-17 20:00

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

      $items = Item::orderBy('name')->get(); 
    
    0 讨论(0)
  • 2020-12-17 20:00

    Hi Please find an answer based on eloquent query laravel

    Table: Users Columns:id,name,class_id

    $users = DB::table('users')->whereIn('class_id', [1, 2, 3])->orderBy('name', 'ASC')->paginate(50);
    
    0 讨论(0)
提交回复
热议问题