I'm working on a Habit Tracker feature for my Web App. There is a week_start_date field for every habit record in the table. The page displays the habits for the current week and now I'm trying to add a functionality where a user can can select the required date from all week_start_dates through a drop down menu and see the records for that week.
In the Index function, I've added this code to enable seeing all dates in a drop down menu:
public function index()
{
//other code
$weekStartDates = HabitTracker::distinct('week_start_date')->pluck('week_start_date')->map(function ($date) {
return \Carbon\Carbon::parse($date)->format('Y-m-d');
});
//other code
return view('habit-tracker', compact('habits','inactiveHabits','weekStartDates'));
}
Next, here's the relevant part from my blade file:
<div class="container">
<h2 class="mt-5 mb-4">Retrieve Previous Records</h2>
<form action="{{ route('habits.showWeek') }}" method="GET">
<div class="mb-3">
<label for="week">Select Week Start Date:</label>
<select class="form-select" name="week" id="week" required>
@foreach($weekStartDates as $startDate)
<option value="{{ $startDate }}">{{ $startDate }}</option>
@endforeach
</select>
</div>
<button type="submit" class="btn btn-primary">Show Habits</button>
</form>
@if($habitsForWeek->isEmpty())
<p>No habits added for this week.</p>
@else
<h2 class="mt-5 mb-4">Habit Tracker for Selected Week</h2>
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Habit</th>
<th scope="col">Monday</th>
<th scope="col">Tuesday</th>
<th scope="col">Wednesday</th>
<th scope="col">Thursday</th>
<th scope="col">Friday</th>
<th scope="col">Saturday</th>
<th scope="col">Sunday</th>
</tr>
</thead>
<tbody>
@foreach($habitsForWeek as $habit)
<tr>
<td>{{ $habit->habit_name }}</td>
<td>{{ $habit->monday ? 'Yes' : 'No' }}</td>
<td>{{ $habit->tuesday ? 'Yes' : 'No' }}</td>
<td>{{ $habit->wednesday ? 'Yes' : 'No' }}</td>
<td>{{ $habit->thursday ? 'Yes' : 'No' }}</td>
<td>{{ $habit->friday ? 'Yes' : 'No' }}</td>
<td>{{ $habit->saturday ? 'Yes' : 'No' }}</td>
<td>{{ $habit->sunday ? 'Yes' : 'No' }}</td>
</tr>
@endforeach
</tbody>
</table>
@endif
</div>
Here's the showWeek controller method I'm using for this task:
public function showWeek(Request $request)
{
$userId = Auth::id();
$selectedWeek = $request->input('week');
// Retrieve habits for the selected week
$habitsForWeek = HabitTracker::where('week_start_date', $selectedWeek)
->where('user_id', $userId)
->get();
//dd($habitsForWeek);
return view('habit-tracker', compact('habitsForWeek'));
}
Here are the routes defined in the web.php file:
Route::middleware('auth')->group(function () {
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
Route::get('/affirmations', [AffirmationsController::class, 'index'])->name('affirmations');
Route::post('/upload-image', [AffirmationsController::class, 'uploadImage'])->name('upload.image');
Route::post('/save-favorite/{affirmationId}', [AffirmationsController::class, 'saveFavoriteAffirmation'])->name('save.favorite.affirmation');
Route::get('/habit-tracker', [HabitTrackerController::class, 'index'])->name('habit.tracker');
Route::post('/habits', [HabitTrackerController::class, 'store'])->name('habits.store');
Route::put('/habits/update-habits', [HabitTrackerController::class, 'updateHabits'])->name('habits.update');
Route::post('/habits/deactivate', [HabitTrackerController::class, 'deactivateHabit'])->name('habits.deactivate');
Route::post('/habits/reactivate', [HabitTrackerController::class, 'reactivateHabit'])->name('habits.reactivate');
Route::get('/habits/week', [HabitTrackerController::class, 'showWeek'])->name('habits.showWeek');
});
Now, in the blade file, if I remove the $habitsForWeek section, the code is working fine as the drop down menu displays the correct values. When I uncomment the dd($habitsForWeek) statement and click the button, I can see the correct values in the $habitsForWeek variable. Which means the route is working fine but I'm getting the following error: Undefined variable $habitsForWeek.
What could be the reason for this? Is it because I'm passing variables from multiple controller methods to the same blade file? Please advice.
Thank you so much for your time!