theGreenCabbage
theGreenCabbage

Reputation: 4845

Formatting JSON data from View in Laravel

I am trying to display my data in my View in this type of illustrated format. The items surrounded by the square brackets are the headers for the table.

[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   ..   ..

My JSON, well, for one class, looks like the following

   {
       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: ""
   }

Essentially, I want to display the instr_type ONCE as a header of the table, and in the table, display the corresponding data. You can assume the data return is ordered by instr_type, so if you have multiple labs and lectures, it will be returned like this - lab, lab, lab, lecture, lecture, etc

I feel like what I want to do would be much easier to achieve if my data was in this format:

   {
       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",
         ..
       }
       ]
   }

My question is how could I format my data within either my Controller or View so that I could achieve this effect? Apologies for the silly question; I feel like the answer to this question is very simple and I am just overthinking it.

Upvotes: 1

Views: 856

Answers (1)

Ben Claar
Ben Claar

Reputation: 3415

I've found using two loops works best for this sort of thing.

// 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

If you want to categorize by additional elements, just add another loop/array level:

// 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

Upvotes: 3

Related Questions