1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Laravel】モデルをall()で取得したときに並び替える方法

Posted at

allで取得したモデルを並び替える方法

モデルをallで取得した時、基本的には主キーでソートされるようです。
別のキーでソートしたい場合でも、order byは使えません。

laravel
$users = Users::all()->orderBy('department_id');

all()で取得した場合は、order byではなくsort byを使う必要があります。

laravel
$users = Users::all()->sortBy('department_id');

参考

Laravel Eloquent: Ordering results of all()

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?