To achieve the desired functionality, you need to modify both the controller and the Blade template. Here's a step-by-step solution:
Step 1: Modify the Controller
First, you need to fetch the overtime signups for the users and the specified dates. You can do this by modifying the index method to include the necessary Eloquent query.
public function index(Request $request)
{
// Check if 'shiftdate' is not in the request and set default value
if (!$request->has('shiftdate')) {
$request['shiftdate'] = Carbon::now()->format('Y-m-d');
}
$shiftdate = $request['shiftdate'];
$submittedDate = Carbon::createFromFormat('Y-m-d', $request->input('shiftdate'));
$date = $submittedDate->format('m/d/Y');
$shiftID = ShiftHelper::getShift($date, '1/1/2023', '1,2,3,4');
$users = User::whereHas('CurrentAssignment', function ($query) use ($shiftID) {
$query->where('shift_id', $shiftID);
})->with('CurrentAssignment')
->where('personnel', 1)
->where('separation_reason', null)
->get()
->groupBy(function ($user) {
return $user->CurrentAssignment->station_id;
})
->sortKeys();
// Increment the shift date by one day
$nextDay = Carbon::createFromFormat('Y-m-d', $shiftdate)->addDay()->format('Y-m-d');
// Eloquent query to get the next six start_dt values greater than the next day after the given shiftDate
$dates = OvertimeShifts::where('start_dt', '>', $nextDay)
->orderBy('start_dt', 'ASC')
->limit(6)
->get(); // Use get() instead of pluck()
// Fetch overtime signups for the users and dates
$signups = OvertimeSignup::whereIn('user_id', $users->flatten()->pluck('id'))
->whereIn('start_dt', $dates->pluck('start_dt'))
->get()
->groupBy('user_id');
return view('stamfordct::overtime/signup/index', compact('users', 'shiftdate', 'dates', 'signups'));
}
Step 2: Modify the Blade Template
Next, you need to update the Blade template to check if a user has signed up for a specific overtime period and display the appropriate image.
@foreach ($users as $station_id => $usersInStation)
<div class="row bg-fusion-50">
<div class="col-12 text-center mt-2">
<h2>Station: {{ $station_id }}</h2>
</div>
</div>
@foreach ($usersInStation as $index => $user)
<div class="row mt-1 mb-1 {{ $index % 2 == 0 ? 'bg-primary-50' : 'bg-white' }}">
<div class="col-1 pt-2">{{ $user->empid }}</div>
<div class="col-5 pt-2">{{ $user->CurrentAssignment->rank->code }} {{ $user->lastname }}, {{ $user->firstname }}</div>
@foreach ($dates as $date)
<div class="col-1">
<div class="d-flex justify-content-center align-items-center p-1 m-1">
@php
$signup = $signups[$user->id]->firstWhere('start_dt', $date->start_dt) ?? null;
@endphp
<img src="{{ $signup ? '/media/images/checkboxes/checked_box.png' : '/media/images/checkboxes/unchecked_box.png' }}" alt=""
style="width:20px;">
<i class="fal fa-edit ml-2 mr-1"></i>
<i class="fal fa-trash ml-2 mr-1"></i>
</div>
</div>
@endforeach
</div>
@endforeach
@endforeach
Explanation
-
Controller Modifications:
- Added a query to fetch overtime signups for the users and the specified dates.
- Grouped the signups by
user_idfor easier access in the Blade template.
-
Blade Template Modifications:
- Used a
@phpdirective to check if a user has signed up for a specific overtime period. - Displayed the appropriate image based on whether a signup record exists.
- Used a
This solution ensures that the Blade template remains clean and the logic is handled in the controller, adhering to the separation of concerns principle.