Есть модель TimeTable:
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();
});
Получаю данные в контроллере:
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'));
}
}
В шаблоне blade вывожу расписание:
<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>
все работает, но смущают две записи
@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
как можно сделать по другому? Или никак?