foreach in blade template not working correctly - php

I'm having issues on displaying my data from foreach loop. I have 400+ thumbs on my database but laravel doesn't not work correctly, and my footer template didn't display too. I will put my code below.
#foreach($thumbs as $thumb)
{{$thumb['name']}}
{{$thumb['desc']}}
{{$thumb['place']}}
#endforeach
myfooter code goes here.
from my controllers
$data['thumbs'] = thumb::all();
return View('tubetour/home',$data);
but when I tried to var_dump or return the value of thumbs on my controller
it displays all my 400+ data.
$data['thumbs'] = thumb::all();
return $data['thumbs'];

You need to pass an associative array as a second argument to the view function:
// Controller
$thumbs = Thumb::all();
return \View::make('tubetour.home', ['thumbs' => $thumbs]);
// View
#foreach($thumbs as $thumb)
{{ $thumb->name }}
{{ $thumb->desc }}
{{ $thumb->place }}
#endforeach
Edit: if you are using laravel 4 you will need to use View::make()

If Thumb is an Eloquent model, then the Thumb::all() will return an Eloquent collection, not an array. In that case you have to update your blade template like so:
#foreach($thumbs as $thumb)
{{ $thumb->name }}
{{ $thumb->desc }}
{{ $thumb->place }}
#endforeach
Hope this solve your issue.
UPDATE
Pass the $thumbs as an array and display it as table.
Update your controller like this:
$data['thumbs'] = thumb::all()->toArray();
return View('tubetour/home', $data);
And your view like this, see how many rows being displayed.
<table>
<thead>
<tr>Id</tr>
<tr>Name</tr>
<td>Desc</td>
<td>Place</td>
</thead>
<tbody>
#for ($i = 0; $i < count($thumbs); $i++)
<tr>
<td>{{ $i }}</td>
<td>{{ $thumbs[$i]['name'] }}</td>
<td>{{ $thumbs[$i]['desc'] }}</td>
<td>{{ $thumbs[$i]['place'] }}</td>
</tr>
#endfor
</tbody>
</table>
UPDATE 2
Blade template example with bootstrap grid:
<div class="row">
#for ($i = 0; $i < count($thumbs); $i++)
<div class="col-md-4">
<img src="img/sample.jpg">
<h3>{{ $thumbs[$i]['name'] }}</h3>
{{ $thumbs[$i]['desc'] }}
</div>
#endfor
</div>

try this
#foreach($data as $thumb)
{{$thumb['name']}}
{{$thumb['desc']}}
{{$thumb['place']}}
#endforeach

Update your controller
$data = Thumb::all();
return View::make('tubetour/home')->with("thumbs",$data)
->render();
Then your view with this:
#foreach($thumbs as $thumb)
{{$thumb['name']}}
{{$thumb['desc']}}
{{$thumb['place']}}
#endforeach

Related

How to add two numbers in the blade Laravel

I need to add two numbers/amount displayed in my blade view, as shown in the image below
I need to display as 666.68 / 1000 AUD
Here's my blade codes
<td>
#foreach ($team->competitionPayments as $payment)
{{ $payment->amount }}
#endforeach
<br>
{{ $team->competitionPayments }}
/
#foreach ($team->competitions as $total)
{{ $total->pivot->total_fee .' AUD'}}
#endforeach
</td>
Here's my controller function
public function detailedRegistrations($competition)
{
$competitions = $this->competitionsRepo->findOrFail($competition);
$teams = $competitions->teams()->get();
$payments = $competitions->payments()->get();
return view('competitions.detailed-registrations',
[
'teams'=>$teams,
'payments'=>$payments,
'competitions'=>$competitions,
]
);
}
Here's the competitionPayments relationship in the Team model
public function competitionPayments()
{
return $this->hasMany(CompetitionPayment::class, 'team_id');
}
Please give me an idea of how to make this work
here competitionPayments is a collection so you can apply sum() function on it like
<td>
{{ $payment->competitionPayments->sum('amount') }}
<br>
ref link https://laravel.com/docs/8.x/collections#method-sum
This works
<td>
{{ $team->competitionPayments->sum('amount') }}
/
#foreach ($team->competitions as $total)
{{ $total->pivot->total_fee .' AUD'}}
#endforeach
</td>

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

for loop with iteration in blade

i would like to know how to do in blade to have the equivalent of this code. I need to do an iteration inside a foreach . i see the blade loop variable like $loop->index or $loop->remaining but I need to know how to use it to make the equivalent of the code below.
<?php
for( $i = 0 ; $i < 3 ; $i++ ) {
$result[$i]['id'];
$result[$i]['name'];
$result[$i]['email'];
}
?>
Thank for any help
Thank mhrabiee. i found the solution.
#foreach($things as $thing)
#if( $loop->first or $loop->iteration <= 3 )
<tr>
<td>{{$thing)->id}}</td>
<td>{{$thing)->name}}</td>
<td>{{$thing)->email}}</td>
</tr>
#endif
#endforeach
this start first iteration
$loop->first
and this stop iteration after 3 loop
$loop->iteration <= 3
and voila !
Your question is a bit vague but exact equivalent of your code is something like this:
#for ($i = 0; $i < 3; $i++)
{{ $result[$i]['id'] }}
{{ $result[$i]['name'] }}
{{ $result[$i]['email'] }}
#endfor
By the way if you want to iterate through something like $results array you can do this:
#foreach ($results as $result)
<div>{{ $result->id }}</div>
<div>{{ $result->name }}</div>
<div>{{ $result->email }}</div>
#endforeach
PS: You can learn more about for loops in Laravel's Blade Documentation.

Getting the position (index) of an object in foreach?

I have a simple foreach block in my view, like below.
#foreach ($teams as $key => $team)
{{ str_ordinal($key + 1) }}
#endforeach
Right now I'm displaying the key, although it isn't exactly accurate. Here's an image:
How can I display the actual position of the current iteration? I order by my teams collection but I'm not sure how I get the position of the current interation in that loop?
You can use $loop->index to get the index. Check its docs. For instance:
#foreach ($teams as $team)
{{ $loop->index }}
#endforeach
Will display 0,1,2,3,4... until the last element position.
You can apply array_values to your data before passing to template:
array_values($teams);
Or, according to this https://laravel.com/docs/5.6/blade#the-loop-variable, you can use special $loop variable. I suppose you need $loop->iteration property (it starts with 1) or $loop->index (starts with 0):
#foreach ($teams as $key => $team)
{{ $loop->iteration }}
#endforeach
#foreach($teams as $team)
<tr>
<td>{{ $loop->index+1}}</td>
<td>{{ $team->name}}</td>
<td>{{ $team->caption}}</td>
<td>{{ $team->address}}</td>
</tr>
#endforeach
Use a for loop instead of a foreach. You will get them in order.
$count = count($teams) ;
for($i=1;$i<=$count;$i++) {
// your code here using $i as the position
}
In Controller
foreach ($partners as $key => $partner) {
$data['id'] = $key + 1;
}
I have solved the same issue with the combination of built in loop variable and php directive
*this is wrong* You cannot use it directly in double curly braces
{{ $loop->index }}
Instead use it like this,
#foreach ($users as $user)
#php($count= $loop->index + 1)
<tr>
<th>{{ $count }}</th>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->created_at }}</td>
</tr>
#endforeach
Though $loop variable is available inside foreach, you cannot use it inside the curly braces in blade template.
use this only it works fine {{$key+1}}

Blade: Undefined Variable Inside Foreach Loop

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)

Categories