Ad

Displaying Results As Single Row

- 1 answer

I'm stuck. The simpliest way to describe my problem will be uploading a screenshot ;)

Absences - index.blade.php

I am implementing absences system. These red badges are displaying when student was absent, and the grey display when student was present.

I want to display these three rows as a single row, because they have the same "created_at" field, but to be honest, I have no idea how to do it. So help me, if you can of course :)

It's my source code:

(CreateAbsencesTable.php)

    public function up()
{
    Schema::create('absences', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('student_id')->unsigned();
        $table->integer('teacher_id')->unsigned();
        $table->integer('lesson')->unsigned();
        $table->string('status');
        $table->string('comment')->nullable();
        $table->timestamps();
    });
}

([email protected])

    /**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    if (auth()->user()->hasRole('student')) {
        $absences = Absence::orderBy('created_at')->where('student_id', auth()->user()->id)->get();

        // dd($absences);

        return view('absences.index', compact('absences'));
    }
}

(resources/views/absences/index.blade.php)

                    @role('student')
                    <table class="table table-borderless table-striped shadow-lg mb-5">
                        <thead>
                            <tr>
                                <th class="align-middle pl-3 w-25">{{ __('Date') }}</th>
                                <th class="align-middle pl-3">{{ __('Lesson') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($absences as $absence)
                                <tr>
                                    <td class="align-middle pl-4 w-25">{{ $absence->created_at }}</td>
                                    <td class="align-middle pl-4">
                                        @for ($i = 1; $i <= 8; $i++)
                                            @if ($i == $absence->lesson)
                                                <span class="badge badge-pill badge-danger my-second-badge">
                                                    {{ $i }}
                                                </span>
                                            @else
                                                <span class="badge badge-pill badge-secondary my-second-badge">
                                                    {{ $i }}
                                                </span>
                                            @endif
                                        @endfor
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                @endrole

Thanks for help. Have a nice day!

Ad

Answer

First in Controller in index() action u need to group $absences by created_at

public function index()
{
    if (auth()->user()->hasRole('student')) {
        return view('absences.index', [
            'absences' => Absence::orderBy('created_at')->where('student_id', auth()->user()->id)->get()->groupBy('created_at')
        ]);
    }
}

after in blade.php run by $absences

<tbody>
@foreach ($absences as $date => $absence)
    <tr>
        <td class="align-middle pl-4 w-25">{{ $date }}</td>
        <td class="align-middle pl-4">
            @for ($i = 1; $i <= 8; $i++)
                @if ($absence->contains($id))
                    <span class="badge badge-pill badge-danger my-second-badge">
                                                    {{ $i }}
                                                </span>
                @else
                    <span class="badge badge-pill badge-secondary my-second-badge">
                                                    {{ $i }}
                                                </span>
                @endif
            @endfor
        </td>
    </tr>
@endforeach
</tbody>
Ad
source: stackoverflow.com
Ad