如何解决Laravel Eager 按原始加载数据顺序不工作或刀片更改顺序?
所以我试图检索数据的一个子集,“按一个日期字段或另一个”预先加载和排序(因为一个日期字段可能为 NULL)。当我在 MysqL 中运行 MysqL 查询(由 Laravel 完全输出)时 - 它以正确的顺序返回行,因此 sql 查询很好。
然而,刀片给出的输出顺序不同。在下面的例子中,它应该是:
- 12 年级 |估计完成日期:2021-12-01
- 电气工程 |完成日期:2018-12-01
- 11 年级 |完成日期:2017-12-01
但结果是:
- 12 年级 |估计完成日期:2021-12-01
- 11 年级 |完成日期:2017-12-01
- 电气工程 |完成日期:2018-12-01
您可以在下面看到控制器,从该代码中,它将变量直接发送到刀片模板,并且刀片中没有其他排序 - 那么为什么要这样做呢? Laravel 是否只是不理解 orderByRaw 并且即使 sql 是合理的,它也没有正确理解它?我该如何解决这个问题
控制器:
DB::enableQueryLog();
$students = $bursary_administrator->students()
->whereHas('bursaries',function($query) use ($request) {
$query->whereYear('conclusion_date','=',$request->year)
->orWhereYear('estimated_conclusion_date',$request->year);
})
->whereIn('status',[1,3,4,6,7])
->with([
'bursaries' => function ($query) use ($request) {
$query->whereYear('conclusion_date',$request->year)
->orWhereYear('estimated_conclusion_date',$request->year);
},'bursaries.enrolments','bursaries.enrolments.courses' => function ($query) use ($request) {
$query->orderByRaw('CASE WHEN completion_date IS NULL THEN estimated_completion_date ELSE completion_date END DESC');
}])
->orderBy('student_name')->orderBy('student_middle_names')->orderBy('student_surname')
->get();
Log::debug(DB::getQueryLog());
[2021-03-10 11:43:48] local.DEBUG: array (
0 =>
array (
'query' => 'select `students`.*,`bursary_administrator_student`.`bursary_administrator_id` as `pivot_bursary_administrator_id`,`bursary_administrator_student`.`student_id` as `pivot_student_id`,`bursary_administrator_student`.`created_at` as `pivot_created_at`,`bursary_administrator_student`.`updated_at` as `pivot_updated_at` from `students` inner join `bursary_administrator_student` on `students`.`id` = `bursary_administrator_student`.`student_id` where `bursary_administrator_student`.`bursary_administrator_id` = ? and exists (select * from `student_bursaries` where `students`.`id` = `student_bursaries`.`student_id` and (year(`conclusion_date`) = ? or year(`estimated_conclusion_date`) = ?)) and `status` in (?,?,?) order by `student_name` asc,`student_middle_names` asc,`student_surname` asc','bindings' =>
array (
0 => 1,1 => '2021',2 => '2021',3 => 1,4 => 3,5 => 4,6 => 6,7 => 7,),'time' => 4.37,1 =>
array (
'query' => 'select * from `student_bursaries` where `student_bursaries`.`student_id` in (1,14,20,24,25,29,41,49,51,53,55,62,64,65,72,76,80,81,85,98,100,133,139,141,156,157,170,199,203,210,214,217,219,224,225,227,230,232,233,234,240,246,254,261,265,272,282,300) and year(`conclusion_date`) = ? or year(`estimated_conclusion_date`) = ?','bindings' =>
array (
0 => '2021','time' => 1.46,2 =>
array (
'query' => 'select * from `student_bursary_enrolments` where `student_bursary_enrolments`.`student_bursary_id` in (1,90,107,124,131,136,184,244,266,283,300,309)','bindings' =>
array (
),'time' => 1.79,3 =>
array (
'query' => 'select * from `student_bursary_enrolment_courses` where `student_bursary_enrolment_courses`.`student_bursary_enrolment_id` in (1,12,19,22,23,28,39,40,54,56,57,66,67,68,73,78,82,83,86,97,99,106,121,122,128,129,137,153,154,155,166,178,179,181,182,183,190,197,198,204,205,206,212,213,215,220,221,226,228,238,247,248,250,251,252,268,269,284,288,289,290,317,340,352) order by CASE WHEN completion_date IS NULL THEN estimated_completion_date ELSE completion_date END DESC','time' => 1.61,)
刀片:
@PHP($row_count=0)
@if ($students)
@foreach ($students as $student)
@PHP($row_count++)
<tr>
<td>{{$row_count}}</td>
<td>{{$student->getStudentFullNameAttribute()}}</td>
<td colspan="3">
<table class="subtable noctal">
<tbody>
@if ($student->bursaries)
@foreach ($student->bursaries as $bursary)
@if ($bursary->enrolments)
@foreach ($bursary->enrolments as $enrolment)
@if ($enrolment->courses)
@foreach ($enrolment->courses as $course)
{{ logger($course) }}
<tr>
<td>{{$course->course}}</td>
<td class="f114">{{$course->completion_date}}</td>
<td class="f114">{{$course->estimated_completion_date}}</td>
</tr>
@endforeach
@endif
@endforeach
@endif
@endforeach
@endif
</tbody>
</table>
</td>
</tr>
@endforeach
@endif
刀片输出:
[2021-03-10 11:43:48] local.DEBUG: {"id":182,"student_bursary_enrolment_id":197,"faculty_type_id":9,"course":"Grade 12","nqf":null,"qualification":null,"current_year":1,"commencement_date":"2019-02-01","estimated_completion_date":"2021-12-01","completion_date":null,"created_at":"2021-03-10T10:34:43.000000Z","updated_at":"2021-03-10T10:34:43.000000Z"}
[2021-03-10 11:43:48] local.DEBUG: {"id":183,"student_bursary_enrolment_id":198,"course":"Grade 11","current_year":null,"commencement_date":"2016-01-01","estimated_completion_date":null,"completion_date":"2017-12-01","updated_at":"2021-03-10T10:34:43.000000Z"}
[2021-03-10 11:43:48] local.DEBUG: {"id":184,"student_bursary_enrolment_id":199,"course":"Electrical Engineering","commencement_date":"2018-01-01","completion_date":"2018-12-01","updated_at":"2021-03-10T10:34:43.000000Z"}
"id" "student_bursary_enrolment_id" "faculty_type_id" "course" "nqf" "qualification" "current_year" "commencement_date" "estimated_completion_date" "completion_date" "created_at" "updated_at"
"183" "198" "9" "Grade 11" \N \N \N "2016-01-01" \N "2017-12-01" "2021-03-10 10:34:43" "2021-03-10 10:34:43"
"184" "199" "9" "Electrical Engineering" \N \N \N "2018-01-01" \N "2018-12-01" "2021-03-10 10:34:43" "2021-03-10 10:34:43"
"182" "197" "9" "Grade 12" \N \N "1" "2019-02-01" "2021-12-01" \N "2021-03-10 10:34:43" "2021-03-10 10:34:43"
发送到 Blade 的数组:
"bursaries":[
{
"id":214,"student_id":214,"registration_date":"2016-09-23","conclusion_date":null,"estimated_conclusion_date":"2021-12-01","arrival_flight_date":"2016-01-01","return_flight_date":null,"subsidy_currency":"USD","subsidy_allowance":"800.00","tuition_value":"17588.98","extended":0,"extension_reason":"","extension_period":"","cancellation_reason":null,"updated_at":"2021-03-10T10:34:43.000000Z","enrolments":[
{
"id":197,"student_bursary_id":214,"academic_institution_campus_id":58,"student_number":"42002165","courses":[
{
"id":182,"updated_at":"2021-03-10T10:34:43.000000Z"
}
]
},{
"id":198,"academic_institution_campus_id":30,"student_number":null,"courses":[
{
"id":183,{
"id":199,"academic_institution_campus_id":3,"student_number":"201717045","courses":[
{
"id":184,"updated_at":"2021-03-10T10:34:43.000000Z"
}
]
}
]
}
]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。