Blade: Undefined Variable Inside Foreach Loop - php

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?

You have a typo
change #foreach ($diaries as $dairy) to #foreach ($diaries as $diary)
and it should work!

There is some typo in var_dump use {{ var_dump($dairy) }}
Try to use compact method for pass data to view as shown below
//return view('app.diary.readDiaries', compact('diaries'));
public function readDiaries($hash)
{
$user = User::where('hash', $hash)
->first();
$diaries = Diary::where('user_id', $user->id)
->get();
return view('app.diary.readDiaries', compact('diaries'));
}

It is just a simple spelling mistake,
#foreach ($diaries as $dairy)
{{ var_dump($diary) }}
#endforeach
in your foreach you are passing $dairy and dumping var name is $diary
chane it to
#foreach ($diaries as $dairy)
{{ var_dump($dairy) }}
#endforeach
Habbit of copy paste is sometimes good for us...
:)

#foreach ($diaries as $dairy)
{{ var_dump($diary) }}
#endforeach
you used ($diaries as $dairy) in foreach
but in foreach you used ($diary)
you want edit this {{ var_dump($diary) }} to {{ var_dump($dairy) }}
or you want edit #foreach ($diaries as $dairy) to #foreach ($diaries as $diary)

Related

How to display array data in laravel view

I have a dataset like below inside a cell named rolls.
[
"142650",
"142651",
"142603",
"142604"
]
I have an array named $rooms, where each room has cell called rolls.
#foreach($rooms as $room)
{{ $room->rolls }}
#endforeach
The code above displaying data like below in view
["142650", "142651", "142603", "142604", "142605"]
But I want to display like below...
142650, 142651, 142603, 142604, 142605
I have tried this
#foreach($rooms as $room)
#foreach($room->rolls as $roll)
{{ $roll }}
#endforeach
#endforeach
But getting error like below
Invalid argument supplied for foreach()
I think the cleanest solution, without changing controller is
#foreach($rooms as $room)
<h1>{{ $room->name }}</h1>
#foreach(json_decode($room->rolls) as $roll)
{{ $roll }}
#endforeach
#endforeach
Try not to use #php or <?php ?> in your blade templates
This is the solution I have found, I have not changed anything in my controller, this is what I have done in the view.
#foreach($rooms as $room)
<h1>{{ $room->name }}</h1>
<?php $rolls = json_decode($room->rolls); ?>
#foreach($rolls as $roll)
{{ $roll }}#if(!$loop->last), #endif
#endforeach
#endforeach

Call to undefined method error when I try to use links()

When I try to use $student->links() I see this error :
Facade\Ignition\Exceptions\ViewException
Call to undefined method App\Student::links()
I checked the controller, model etc but all of them seem OK... How can I fix this?
(I tried this code both on my Macbook and VPS -CentOS7- but same problem occurs)
That part of my view looks like this:
</tr>
#endforeach
</tbody>
</table>
{{ $student->links() }}
</div>
#endsection
Change
{{ $student->links() }}
to
{{ $students->links() }}
(use plural form).
You need to paginate in your backend code. $students = App\Student::paginate(15);
And then you can access the links()
<div class="container">
#foreach ($students as $student)
{{ $student->name }}
#endforeach
</div>
{{ $students->links() }}

How to fix Undefined offset: 0 in laravel blade?

I am fetching records with pagination. The first page is working fine but when I click on page number 2 then it return following error
Undefined offset: 0
I am trying following script in controller
$Jobs = Jobs::orderBy('job_id', 'asc')->paginate(5);
return view('veteran.job-posting.index',['Jobs'=>$Jobs]);
In the blade following code I am trying
#if($Jobs)
#foreach($Jobs as $Job)
{{ ucfirst(trans($Job->job_title)) }}
#endforeach
#endif
For pagination I am using following script
<div class="pagination-test">
{{ $Jobs->links() }}
</div>
Please guide me why it does returning error when I click on page number 2. I would like to appreciate
After Request For More Detail from #pseudoanime
For more detail about error
2/2) ErrorException
Undefined offset: 0 (View: E:\xampp\htdocs\project\project-sub-root\resources\views\index.blade.php)
Try adding this outside of the loop
{{ $Jobs->links() }}
Can you try to add Pagination like this.
#if(count($Jobs) > 0)
#foreach($Jobs as $Job)
{{ ucfirst(trans($Job->job_title)) }}
#endforeach
{{ $Jobs->links() }}
#endif
With your CSS class
#if(count($Jobs) > 0)
#foreach($Jobs as $Job)
{{ ucfirst(trans($Job->job_title)) }}
#endforeach
<div class="pagination-test">
{{ $Jobs->links() }}
</div>
#endif
Edited Update
Try With With in controller
return view('veteran.job-posting.index')->with('Jobs',$Jobs);
Update your controller's function return and use compact function for to pass jobs to view.
$jobs = Job::orderBy("id", "asc")->paginate(5);
return view("jobs", compact("jobs"));
You need to add this for pagination in blade.
<ul class="pagination" >
{!! $Jobs->render() !!}
</ul>

How can I define variable array on the laravel blade view?

I try like this :
<div class="media-body">
#foreach($categories as $category)
#php $category[] = $category->name #endphp
#endforeach
{{ implode(",", $category) }}
</div>
If the code executed, there exist error :
undefine variable category
How can I solve it?
You can simply use Laravel Collection
{{ $categories->pluck('name')->implode(', ') }}
Or if you wanna do this in foreach then
#php ($names = [])
#foreach ($categories as $category)
#php ($names[] = $category->name)
#endforeach
{{ implode(', ', $names) }}
You have to declare an array within <?php ... ?> block and then use the same in a {{blade}} block.

Blade if(isset) is not working Laravel

Hi I am trying to check the variable is already set or not using blade version. But the raw php is working but the blade version is not. Any help?
controller:
public function viewRegistrationForm()
{
$usersType = UsersType::all();
return View::make('search')->with('usersType',$usersType);
}
view:
{{ $usersType or '' }}
it shows the error :
Undefined variable: usersType (View: C:\xampp\htdocs\clubhub\app\views\search.blade.php)
{{ $usersType or '' }} is working fine. The problem here is your foreach loop:
#foreach( $usersType as $type )
<input type="checkbox" class='default-checkbox'> <span>{{ $type->type }}</span>
#endforeach
I suggest you put this in an #if():
#if(isset($usersType))
#foreach( $usersType as $type )
<input type="checkbox" class='default-checkbox'> <span>{{ $type->type }}</span>
#endforeach
#endif
You can also use #forelse. Simple and easy.
#forelse ($users as $user)
<li>{{ $user->name }}</li>
#empty
<p>No users</p>
#endforelse
#isset($usersType)
// $usersType is defined and is not null...
#endisset
For a detailed explanation refer documentation:
In addition to the conditional directives already discussed, the #isset and #empty directives may be used as convenient shortcuts for their respective PHP functions
Use ?? , 'or' not supported in updated version.
{{ $usersType or '' }} ❎
{{ $usersType ?? '' }} ✅
Use 3 curly braces if you want to echo
{{{ $usersType or '' }}}
On Controller
$data = ModelName::select('name')->get()->toArray();
return view('viewtemplatename')->with('yourVariableName', $data);
On Blade file
#if(isset($yourVariableName))
//do you work here
#endif
You can use the ternary operator easily:
{{ $usersType ? $usersType : '' }}
#forelse ($users as $user)
<li>{{ $user->name }}</li>
#empty
<p>No users</p>
#endforelse
Use ?? instead or {{ $usersType ?? '' }}
I solved this using the optional() helper. Using the example here it would be:
{{ optional($usersType) }}
A more complicated example would be if, like me, say you are trying to access a property of a null object (ie. $users->type) in a view that is using old() helper.
value="{{ old('type', optional($users)->type }}"
Important to note that the brackets go around the object variable and not the whole thing if trying to access a property of the object.
https://laravel.com/docs/5.8/helpers#method-optional

Categories