在 laravel 中按键按数组排序
Order by an array by key in laravel
我正在使用下面的查询从以下 tables 中获取匹配的 ID,我将只获得 student_id
.
有了这个 student_id
,我必须从 students
table 中找到匹配的行,然后我想 orderBy()
来自 students
table.
我建立了student_mapping
和student
模型之间的关系。
$mapps = Student_mapping::select('student_id');
if($request->session_id) {
$mapps = $mapps->where('session', '=', $request->session_id);
}
if($request->class_id) {
$mapps = $mapps->where('class_id', '=', $request->class_id);
if($request->group_id) {
$mapps = $mapps->where('group_id', '=', $request->group_id);
if($request->section_id) {
$mapps = $mapps->where('section_id', '=', $request->section_id);
}
}
}
$mapps = $mapps->get();
$students = [];
foreach($mapps as $map) {
if($map->student)
{
$students[] = Student::find($map->student_id);
}
}
我必须让 $students->orderBy('name', 'ASC'), 但我做不到。
可以使用User::where('id', $map->student_id)->orderBy('name', 'ASC')
;这看起来没什么用,因为 id
无论如何都是独一无二的。
另外,您的代码似乎会受到多次调用数据库的影响。
您可以将查询简化为:
$student_ids = $mapps->pluck('student_id');
$students = Student::whereIn('id', $student_ids)->orderBy('name', 'ASC')->get();
PS: Eloquent (Model) wraps around QueryBuilder. See example of ordering or groupby in the documentation
这样查询会更快更好:
$students = Student::whereIn('id', $mapps)->orderBy('id')->get();
这将为您提供所有学生的 Collection
,应按 ID 排序。如果你想把它作为一个数组,记得调用集合上的 ->toArray()
方法,但无论哪种方式它都应该作为一个集合来实现它的目的。
$students = [];
foreach($mapps as $map) {
if($map->student)
{
$students[] = Student::find($map->student_id);
}
}
来自 laravel collection class see
return collect($student)->sortBy('name')->values()->all();
我正在使用下面的查询从以下 tables 中获取匹配的 ID,我将只获得 student_id
.
有了这个 student_id
,我必须从 students
table 中找到匹配的行,然后我想 orderBy()
来自 students
table.
我建立了student_mapping
和student
模型之间的关系。
$mapps = Student_mapping::select('student_id');
if($request->session_id) {
$mapps = $mapps->where('session', '=', $request->session_id);
}
if($request->class_id) {
$mapps = $mapps->where('class_id', '=', $request->class_id);
if($request->group_id) {
$mapps = $mapps->where('group_id', '=', $request->group_id);
if($request->section_id) {
$mapps = $mapps->where('section_id', '=', $request->section_id);
}
}
}
$mapps = $mapps->get();
$students = [];
foreach($mapps as $map) {
if($map->student)
{
$students[] = Student::find($map->student_id);
}
}
我必须让 $students->orderBy('name', 'ASC'), 但我做不到。
可以使用User::where('id', $map->student_id)->orderBy('name', 'ASC')
;这看起来没什么用,因为 id
无论如何都是独一无二的。
另外,您的代码似乎会受到多次调用数据库的影响。
您可以将查询简化为:
$student_ids = $mapps->pluck('student_id');
$students = Student::whereIn('id', $student_ids)->orderBy('name', 'ASC')->get();
PS: Eloquent (Model) wraps around QueryBuilder. See example of ordering or groupby in the documentation
这样查询会更快更好:
$students = Student::whereIn('id', $mapps)->orderBy('id')->get();
这将为您提供所有学生的 Collection
,应按 ID 排序。如果你想把它作为一个数组,记得调用集合上的 ->toArray()
方法,但无论哪种方式它都应该作为一个集合来实现它的目的。
$students = [];
foreach($mapps as $map) {
if($map->student)
{
$students[] = Student::find($map->student_id);
}
}
来自 laravel collection class see
return collect($student)->sortBy('name')->values()->all();