如何在Laravel中按字母顺序对记录进行排序
问题描述:
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?
如何在laravel中按字母顺序对记录进行排序? p>
public function index()
{
$ comproducts = Comproduct :: paginate(3);
$ items = Item :: orderBy('name') - > all();
返回视图('computer',compact(['comproducts','items']));
}
code> pre>
这不是 工作正常。 这显示 p>
调用未定义的方法Illuminate \ Database \ Query \ Builder :: all() p>
blockquote>
这个错误。 我该如何解决这个问题? p>
div>
答
I use get()
instead , you can't modify query with method all()
and also it is static function
$items = Item::orderBy('name')->get();
答
That's how you sort it, orderBy()
comes after all()
:
$items = Item::all()->sortBy('name');
Reference: https://laravel.com/docs/5.5/collections
答
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);