I have a controller which gets an array of a User's diaries from my database and passes them to my view:
<?php
public function readDiaries($hash)
{
$user = User::where('hash', $hash)->first();
$diaries = Diary::where('user_id', $user->id)->get();
return view('app.diary.readDiaries', ['diaries' => $diaries]);
}
In my view, I am looping through the diaries using a @foreach
loop.
<div id="diaries" class="card-columns">
@if (count($diaries) > 0)
@foreach ($diaries as $dairy)
{{ var_dump($diary) }}
@endforeach
@endif
</div>
But I am getting the following undefined variable error...
Undefined variable: diary (View: C:\xampp\htdocs\personal_projects\Active\diary_app\resources\views\app\diary\readDiaries.blade.php)
Why is my $diary variable undefined inside the @foreach
loop?
$dairy
and in vardump$diary
– Ritesh Khatri