格式化来自 Laravel 视图中的 JSON 数据
Formatting JSON data from View in Laravel
我正在尝试以这种插图格式在我的视图中显示我的数据。方括号中的项目是 table 的 header。
[Lecture]
ECE 201 Section 1 .. ..
ECE 201 Section 2 .. ..
ECE 201 Section 3 .. ..
[Lab]
ECE 201 Section 10 .. ..
ECE 201 Section 11 .. ..
ECE 201 Section 12 .. ..
[Recitation]
ECE 201 Section 20 .. ..
ECE 201 Section 21 .. ..
ECE 201 Section 22 .. ..
我的 JSON,嗯,对于一个 class,看起来像下面的
{
year: "2015",
term: "Summer",
subject_code: "ECE",
course_no: "201",
instr_type: "Lecture",
instr_method: "Face To Face",
section: "003",
crn: "42953",
course_title: "Digital Logic",
credits: "3.0",
day: "R",
time: "06:30 pm - 09:20 pm",
instructor: "Teacher Name",
campus: "University Building",
max_enroll: "18",
enroll: "18",
building: "PLACE",
room: null,
description: "Introduction to logic for computing",
pre_reqs: "",
co_reqs: ""
}
本质上,我想把instr_type
ONCE显示为table的一个header,并在table中显示相应的数据。您可以假设数据 return 按 instr_type
排序,因此如果您有多个实验室和讲座,它将像这样 returned - 实验室,实验室,实验室,讲座,讲座,等等
我觉得如果我的数据采用这种格式,我想做的事情会更容易实现:
{
year: "2015",
term: "Summer",
subject_code: "ECE",
course_no: "201",
instr_type: "Lecture",
info: [
{
instr_method: "Face To Face",
section: "003",
crn: "42953",
course_title: "Digital Logic",
credits: "3.0",
day: "R",
time: "06:30 pm - 09:20 pm",
instructor: "Teacher Name",
campus: "University Building",
max_enroll: "18",
enroll: "18",
building: "PLACE",
room: null,
description: "Introduction to logic for computing",
pre_reqs: "",
co_reqs: ""
}, {
instr_method: "Face To Face",
section: "004",
crn: "42954",
..
}
]
}
我的问题是如何在我的 Controller 或 View 中格式化我的数据,以便实现这种效果?为这个愚蠢的问题道歉;我觉得这个问题的答案很简单,只是我想多了。
我发现使用两个循环最适合这种事情。
// in your Controller
$classesByType = [];
foreach ($classes => $class) {
$classesByType[$class['instr_type']][] = $class;
}
return view('my-view', ['classesByType' => $classesByType]);
// In your Blade template
@foreach ($classesByType as $type => $classes)
<h1>{{ $type }}</h1>
@foreach ($classes as $class)
<p>{{ $class['subject_code'] }} {{ $class['course_code'] }} ...</p>
@endforeach
@endforeach
如果要按其他元素分类,只需添加另一个 loop/array 级别:
// in your Controller
$classesByLabelAndType = [];
foreach ($classes => $class) {
$label = $class['subject_code'] . " " . $class['course_no'];
$classesByLabelAndType[$label][$class['instr_type']][] = $class;
}
return view('my-view', ['classesByLabelAndType' => $classesByLabelAndType]);
// In your Blade template
@foreach ($classesByLabelAndType as $label => $classesByType)
<h1>{{ $label }}</h1>
@foreach ($classesByType as $type => $classes)
<h2>{{ $type }}</h2>
@foreach ($classes as $class)
<p>{{ $class['course_code'] }} ...</p>
@endforeach
@endforeach
@endforeach
我正在尝试以这种插图格式在我的视图中显示我的数据。方括号中的项目是 table 的 header。
[Lecture]
ECE 201 Section 1 .. ..
ECE 201 Section 2 .. ..
ECE 201 Section 3 .. ..
[Lab]
ECE 201 Section 10 .. ..
ECE 201 Section 11 .. ..
ECE 201 Section 12 .. ..
[Recitation]
ECE 201 Section 20 .. ..
ECE 201 Section 21 .. ..
ECE 201 Section 22 .. ..
我的 JSON,嗯,对于一个 class,看起来像下面的
{
year: "2015",
term: "Summer",
subject_code: "ECE",
course_no: "201",
instr_type: "Lecture",
instr_method: "Face To Face",
section: "003",
crn: "42953",
course_title: "Digital Logic",
credits: "3.0",
day: "R",
time: "06:30 pm - 09:20 pm",
instructor: "Teacher Name",
campus: "University Building",
max_enroll: "18",
enroll: "18",
building: "PLACE",
room: null,
description: "Introduction to logic for computing",
pre_reqs: "",
co_reqs: ""
}
本质上,我想把instr_type
ONCE显示为table的一个header,并在table中显示相应的数据。您可以假设数据 return 按 instr_type
排序,因此如果您有多个实验室和讲座,它将像这样 returned - 实验室,实验室,实验室,讲座,讲座,等等
我觉得如果我的数据采用这种格式,我想做的事情会更容易实现:
{
year: "2015",
term: "Summer",
subject_code: "ECE",
course_no: "201",
instr_type: "Lecture",
info: [
{
instr_method: "Face To Face",
section: "003",
crn: "42953",
course_title: "Digital Logic",
credits: "3.0",
day: "R",
time: "06:30 pm - 09:20 pm",
instructor: "Teacher Name",
campus: "University Building",
max_enroll: "18",
enroll: "18",
building: "PLACE",
room: null,
description: "Introduction to logic for computing",
pre_reqs: "",
co_reqs: ""
}, {
instr_method: "Face To Face",
section: "004",
crn: "42954",
..
}
]
}
我的问题是如何在我的 Controller 或 View 中格式化我的数据,以便实现这种效果?为这个愚蠢的问题道歉;我觉得这个问题的答案很简单,只是我想多了。
我发现使用两个循环最适合这种事情。
// in your Controller
$classesByType = [];
foreach ($classes => $class) {
$classesByType[$class['instr_type']][] = $class;
}
return view('my-view', ['classesByType' => $classesByType]);
// In your Blade template
@foreach ($classesByType as $type => $classes)
<h1>{{ $type }}</h1>
@foreach ($classes as $class)
<p>{{ $class['subject_code'] }} {{ $class['course_code'] }} ...</p>
@endforeach
@endforeach
如果要按其他元素分类,只需添加另一个 loop/array 级别:
// in your Controller
$classesByLabelAndType = [];
foreach ($classes => $class) {
$label = $class['subject_code'] . " " . $class['course_no'];
$classesByLabelAndType[$label][$class['instr_type']][] = $class;
}
return view('my-view', ['classesByLabelAndType' => $classesByLabelAndType]);
// In your Blade template
@foreach ($classesByLabelAndType as $label => $classesByType)
<h1>{{ $label }}</h1>
@foreach ($classesByType as $type => $classes)
<h2>{{ $type }}</h2>
@foreach ($classes as $class)
<p>{{ $class['course_code'] }} ...</p>
@endforeach
@endforeach
@endforeach