Answer the question
In order to leave comments, you need to log in
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();
});
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'));
}
}
<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
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question