O
O
OlegSedoy2020-08-18 05:32:48
Laravel
OlegSedoy, 2020-08-18 05:32:48

Club schedule in blade template?

There is a TimeTable model:

Schema::create('timetables', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('day');
            $table->time('start');
            $table->time('end');
            $table->unsignedBigInteger('lesson_id');
            $table->foreign('lesson_id')->references('id')->on('lessons');
            $table->unsignedBigInteger('trainer_id');
            $table->foreign('trainer_id')->references('id')->on('trainers');
            $table->boolean('appointment_only')->nullable();
            $table->boolean('check_cost')->nullable();
            $table->timestamps();
        });

I get data in the controller:
class TimetableController extends Controller
{
    public function __invoke(Request $request)
    {
        $timetable = Timetable::with(['trainer', 'lesson'])->get();

        $times = $timetable->unique('start')->pluck(['start'])->sort();

        $days = Timetable::getDays(); //возвращает массив дней с понедельника по воскресенье

        return view('public.timetable.index', compact('timetable', 'days', 'times'));
    }
}

In the blade template, I display the schedule:
<table class="table">
                <thead>
                <tr>
                    <th>Time</th>
                    @foreach($days as $day)
                        <th>{{$day}}</th>
                    @endforeach
                </tr>
                </thead>
                <tbody>
                @foreach($times as $time)
                    <tr>
                        <td>{{$time}}</td>
                        @foreach($days as $day)
                            <td>
                                @if($timetable->contains(function ($value, $key) use ($time, $day) {
                                    return $value->start == $time && $value->day == $day;
                                }))

                                    @php
                                        $lessons = $timetable->filter(function ($item, $key) use ($time, $day) {
                                        return $item->start == $time && $item->day == $day;
                                            });
                                    @endphp

                                    @foreach($lessons as $item)

                                        <div class="card @if ($loop->last) mt-3 @endif" data-id="{{$item->id}}"
                                             style="background-color: {{$item->lesson->color}}">
                                            <div class="card-body">
                                                @if($item->appointment_only)
                                                    <i class="far fa-clock" data-toggle="tooltip" data-placement="top" title="Только по записи"></i>
                                                @endif

                                                @if($item->check_cost)
                                                        <i class="fas fa-ruble-sign" data-toggle="tooltip" data-placement="top" title="Стоимость абонемента уточняйте у администратора"></i>
                                                @endif
                                                <h5 class="card-title">{{$item->lesson->name}}</h5>
                                                <p class="card-text">{{$item->start}}-{{$item->end}}</p>
                                                <p class="card-text">{{$item->trainer->name}}</p>
                                            </div>
                                        </div>

                                    @endforeach

                                @endif
                            </td>
                        @endforeach
                    </tr>
                @endforeach

                </tbody>
            </table>

everything works, but two entries confuse
@if($timetable->contains(function ($value, $key) use ($time, $day) {
return $value->start == $time && $value->day == $day;
}))

and
@php
$lessons = $timetable->filter(function ($item, $key) use ($time, $day) {
return $item->start == $time && $item->day == $day;
});
@endphp

how can it be done differently? Or not at all?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daria Motorina, 2020-08-18
@OlegSedoy

It is worth moving the preparation of this data into the controller action (and in order not to overload the action, then move these filters to private methods or a separate service in general), then the template will have a simple data output. @php in a blade this is an extreme measure)) in other template engines such constructions are prohibited

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question