academy/app/Http/Controllers/TimetableController.php

42 lines
1.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Timetable;
use Carbon\Carbon;
class TimetableController extends Controller
{
public function index()
{
$studentLogId = session('student_log_id') ?? session('student_id');
if (!$studentLogId) {
return redirect('/signin')->with('error', 'Please login first!');
}
// DB එකේ නැති student_log_id සහ sort_order query එකෙන් ඉවත් කර ඇත
$rawTimetables = Timetable::all();
$schedule = [];
$timeSlots = [];
foreach ($rawTimetables as $item) {
if (!in_array($item->time_slot, $timeSlots)) {
$timeSlots[] = $item->time_slot;
}
$schedule[$item->time_slot][$item->day] = $item;
}
// Today's Schedule (දැනට ඇති columns වලින් පමණක් Filter කර ඇත)
$todayName = Carbon::now()->format('l');
$todaySchedule = Timetable::where('day', $todayName)
->where('type', '!=', 'Break')
->get();
$days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
return view('timetable', compact('schedule', 'timeSlots', 'days', 'todaySchedule', 'todayName'));
}
}