bashiro's avatar

NEED HELP TO CATEGORIZE SUB HEADING!

Hello Folks, I have the following in a database- short format

YEAR NAME Subject Grade 2022 John English Pass 2023 John Maths Pass 2022 John Physical Edu Fail

I want the report or blade to look like below Year comes as sub heading then followed the exams as below;

REPORT

Name: John

Subject Grade

Year: 2022
--------

English Pass Phys Edu Fail

Year 2023

Maths Pass

I passed everythig to blade where I have the following

foreach ($singleStudent as $key =>$student )

But I cannot seem to pull the year to look as above. Any idea or code snip ? Please...

0 likes
13 replies
Snapey's avatar

Your post SHOUTED OUT AT ME when I visited the forum, and DESPITE your best efforts, I have no idea what you are asking.

If there are supposed to be blocks of code in your post, try formatting them.

3 likes
rodrigo.pedra's avatar
<?php // ./routes/web.php

use App\Models\Student;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('report', [
        'singleStudent' => Student::query()->where('name', 'John')->get(),
    ]);
});
{{-- ./resources/views/report.blade.php --}}
<h1>Report</h1>

<p>
    <b>Student:</b> {{ $singleStudent->first()?->name ?? 'UNKNOWN' }}
</p>

@foreach($singleStudent->groupBy('year')->sortKeys() as $year => $records)
    <h2>{{ $year }}</h2>

    <ul>
        @foreach($records as $record)
            <li>
                {{ $record->subject }}: <b>{{ $record->grade }}</b>
            </li>
        @endforeach
    </ul>
@endforeach
<?php // ./database/migrations/2024_07_12_022401_create_students_table.php

use App\Models\Student;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    public function up(): void
    {
        Schema::create('students', function (Blueprint $table) {
            $table->id();
            $table->unsignedSmallInteger('year');
            $table->string('name');
            $table->string('subject');
            $table->char('grade', 4);
            $table->timestamps();
        });
    }
};
<?php // ./database/seeders/DatabaseSeeder.php

namespace Database\Seeders;

use App\Models\Student;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        Student::query()->forceCreate([
            'year' => 2022,
            'name' => 'John',
            'subject' => 'English',
            'grade' => 'PASS',
        ]);

        Student::query()->forceCreate([
            'year' => 2023,
            'name' => 'John',
            'subject' => 'Maths',
            'grade' => 'PASS',
        ]);

        Student::query()->forceCreate([
            'year' => 2022,
            'name' => 'John',
            'subject' => 'Physical Edu',
            'grade' => 'FAIL',
        ]);
    }
}

Screenshot_20240711_233649

1 like
rodrigo.pedra's avatar

As @snapey said, the way you formatted your post makes it very challenging to understand.

I tried my best and hope it helps you out.

On the text box where you wrote your post, or can write any replies, there is a notice at the bottom saying:

You may use Markdown with GitHub-flavored code blocks.

Take a look at the linked page to learn how to format your code properly. That will make it easier for people to help you without guessing.

https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax

bashiro's avatar

@rodrigo.pedra

Your really did a great job. Thanks a lot The code works very fine. I am really happy. Thanks once again. Any way to get it in a table format ? I tried but I get only the 2022 data in the table but 2023 fall outside the table. Any idea ?

rodrigo.pedra's avatar

@bashiro

It is very hard to known what table format you refer to, specially when the thread's title asks for help to categorize sub-heading

What does sub-heading mean in a table context?

rodrigo.pedra's avatar

Hope this is what you mean. Note that for the sake of simplicity, I am using deprecated table attributes (border and valign)

{{-- ./resources/views/report.blade.php --}}
<h1>Report</h1>

<p>
    <b>Student:</b> {{ $singleStudent->first()?->name ?? 'UNKNOWN' }}
</p>

<table border="1">
    <thead>
    <tr>
        <th>Year</th>
        <th>Subject</th>
        <th>Grade</th>
    </tr>
    </thead>
    @foreach($singleStudent->groupBy('year')->sortKeys() as $year => $records)
        <tbody>
        <tr>
            <th scope="row" rowspan="{{ $records->count() }}" valign="top">{{ $year }}</th>

            @foreach($records as $record)
                <td>{{ $record->subject }}</td>
                <td>{{ $record->grade }}</td>
                </tr>
                @unless($loop->last)
                    <tr>
                @endunless
            @endforeach
        </tr>
        </tbody>
    @endforeach
</table>

Screenshot_20240715_061842

bashiro's avatar

@rodrigo.pedra thats what I did!

  @foreach($singleStudent->groupBy('year')->sortKeys() as $year => $records)
 
  <h6  class="text-center"> <b>{{ $year }}</b> </h6>


      @foreach($records as $record)
      <tbody>
        <tr>        
               <td>{{ $record->subject }} </td>
             <td> {{ $record->grade }} </td>
        </tr>   
        </tbody> 
      @endforeach
 

@endforeach

rodrigo.pedra's avatar
Level 56

@bashiro where is your <table> tags?

Do you want the subheading in the middle of the table?

Or one table for each year?

Try this:

{{-- ./resources/views/report.blade.php --}}
<h1>Report</h1>

<p>
    <b>Student:</b> {{ $singleStudent->first()?->name ?? 'UNKNOWN' }}
</p>

@foreach($singleStudent->groupBy('year')->sortKeys() as $year => $records)
    <h2>{{ $year }}</h2>

    <table border="1">
        <thead>
        <tr>
            <th>Subject</th>
            <th>Grade</th>
        </tr>
        </thead>
        <tbody>
        @foreach($records as $record)
            <tr>
                <td>{{ $record->subject }}</td>
                <td>{{ $record->grade }}</td>
            </tr>
        @endforeach
        </tbody>
    </table>
@endforeach

Screenshot_20240715_202540

bashiro's avatar

@rodrigo.pedra So beautiful. I have really learnt a lot from you! And not to say the least , from here. I will really love to buy you a cup of coffee. Thanks a lot. It worked perfectly. I am grateful. Thanks once again.

1 like
bashiro's avatar

Hello all, Thanks a lot for the inputs. I was away for some couple of days thats why I was not able to explain the questions folk asked.

Rodrigo thanks a lot for the code. I will try that and see the outcome. Some of you asked me to explain myself especially Snapy.

Here is what I meant. I have data base example of different students with subjects and grade. If one choose to view John as an example

I need a code that can sort John and arrange it according to the year the exams was taken. In other words categorise the grades under years

REPORT John Terminal Exams

Year 2022

Subject Grade ,

Mathematics C English B ,

Year 2023

Subject Grad -,

Physical Edu Pass

I cannot format my text to show here but hope its readable

Thanks everyone

Please or to participate in this conversation.