Hello guys i have declared my posts with variable $lastItems
#if($lastItems->total() > 0)
#foreach($lastItems as $item)
#include('._posting.items')
#endforeach
#else
#include('errors.emptycontent')
#endif
and i want to add ads after each 3 post, i use something like this
#if($lastItems->total() > 0)
#foreach($lastItems as $item)
#include('._posting.items')
#endforeach
#if($item = 3)
<img src="link" alt="ads">
#endif
#else
#include('errors.emptycontent')
#endif
but the problem is, when i try like this it shows the ads after every post not after 3 posts.
What i'm doing wrong?
You may try something like:
#if($lastItems->total() > 0)
#foreach($lastItems as $key => $item)
#include('._posting.items')
#if (($key + 1) % 3 == 0)
<img src="link" alt="ads">
#endif
#endforeach
#else
#include('errors.emptycontent')
#endif
I had almost same problem, in my case I had to change div's class. So I used sth like this and it helped me:
<?php $var = -1; ?>
#foreach($lastItems as $item)
<?php $var ++;?>
#if($var%3 == 0 && $var!=0)
#include('errors.emptycontent')
else
#include('._posting.items')
#endif
#endforeach
I hope it helps :)
Related
I've just started using the framework. In plain PHP after the opening foreach I would then set the variables then close the php tag but then from what I can work out you have to then do the Laravel #foreach tags and then open and close #php. Is there a way around this as it seems like a lot of extra work and code?
#foreach($steps as $row)
#php
$title = $row->title;
$text = $row->text;
$i = 1;
#endphp
<div class="steps-item grid-wrap">
<div class="number"
#if($text || $title)
<div class="text-wrap">
#if($title)
<h2>{{$title}}</h2>
#endif
{!! $text !!}
</div>
#php
$i++;
#endphp
#endif
</div>{{--END steps-item--}}
#endforeach
Since blade is no PHP, you have to return to PHP with that directive. But you can set/use the variables without doing that in your case:
#foreach($steps as $i => $row)
<div class="steps-item grid-wrap">
<div class="number"
#if($text || $title)
<div class="text-wrap">
#if($title)
<h2>{{ $row->title }}</h2>
#endif
{!! $row->text !!}
</div>
#php
$i++;
#endphp
#endif
</div>{{--END steps-item--}}
#endforeach
If you still want to set variables, there's a Laravel package called alexdover/blade-set. But as #brombeer pointed out, in most cases it's highly recommended to set all necessary variables in the controller before passing them to the view.
Use laravel provided loop variables:
$loop->iteration The current loop iteration (starts at 1).
It will increment in every loop iteration automatically.
e.g:
First iteration = $loop->iteration => 1 ;
Second iteration = $loop->iteration => 2 ;
so on until loop ends.
Check docs:
The Loop Variables
You can use a #for directive with sizeof($steps) like that:
#for($i=0; $i<= sizeof($steps)-1; $i++)
#endfor
#foreach ($steps as $row)
<div class="steps-item grid-wrap">
<div class="number">
<div class="text-wrap">
#if ($row->title != '')
<h2>{{$row->title}}</h2>
/* if you want to display another title when its blank you can
use if-else here otherwise not need to use if conditions for
title and text */
#endif
#if ($row->text != '')
{!! $row->text !!}
#endif
</div>
</div>
</div>
#endforeach
{{--
for an example you have $steps values like
$steps =
Array(
[0] -> 1,
[1] -> 'title',
[2] -> 'text'
);
if you want this array key value pair you have to use #foreach like
#foreach ($steps as $key=>$value)
#endforeach
you can use key value in #foreach loop only
--}}
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.
I am calling categories collection from controller and displaying in the blade in foreach loops
#foreach ($categories as $category)
#foreach ($category->subcategories as $subcategory)
<a class="a.toggle-vis" data-column="1">{{ $subcategory->name }}</a>
#endforeach
#endforeach
I need to add index numbers generated in the loop from 1 in data-column value
data-column="1"
data-column="2"
data-column="3"
so on....in
For doing this you need to make a variable for counting after that you should pass that variable to view like below.
I am inside a method
public function getSingle($slug){
$category= Post::where('slug','=',$slug)->first();
if ($post != null) {
$counter = 0;
return view('blog.single')->withCategories($category)->withCounter($counter);
} else {
return view('error.error404');
}
}
After that you should access that Counter variable in view like below
#foreach ($categories as $category)
#foreach ($category->subcategories as $subcategory)
<a class="a.toggle-vis" data-column="{{$counter++}}">{{ $subcategory->name }}</a>
#endforeach
#endforeach
The classic for will do
#foreach ($categories as $category)
#for ($i = 0; $i < count($category->subcategories); $i++)
<a class="a.toggle-vis" data-column="{{$i}}">{{ $category->subcategories[$i]->name }}</a>
#endfor
#endforeach
For a shorter one, I think, we can do something like this. A little bit dirty in view but in the small snippet, it should be okay.
{{ !($index = 1) }}
#foreach ($categories as $category)
#foreach ($category->subcategories as $subcategory)
<a class="a.toggle-vis" data-column="{{ $index++ }}">{{ $subcategory->name }}</a>
#endforeach
#endforeach
Use this simple solution (Laravel method):
$loop->iteration The current loop iteration (starts at 1).
It will automatically increment, in every loop iteration.
Check docs:
The Loop Variable
I have a table called users and I have row called ELO_points.
Some users have "-10" ELO_points and some have "25".
Sometimes orderBy not working correctly.
My controller:
$users = User::orderBy('ELO_points', 'DESC')->take(5)->get();
My view:
#foreach ($users as $user)
#if($loop->iteration == 1)
<div class="right-points">{!! $user->ELO_points !!}</div>
</li>
#endif
#if($loop->iteration == 2)
<div class="right-points">{!! $user->ELO_points !!}</div>
</li>
#endif
#endforeach
Any help why my ordering not showing like normal from -10 to 10?
Try it with the following query.
$query = "CAST(ELO_points AS INT) DESC";
$users = User::orderByRaw($query)->take(5)->get();
There may be 5 or 6 values inside the foreach loop but i need to print suppose first 5 or 6 values.How do i do that?
<div class="tag-area">
#foreach(explode(',',$product->tags) as $tag)
<span>{{$tag}}</span>
#endforeach
</div>
You should try this:
<div class="tag-area">
#foreach(explode(',',$product->tags) as $key => $tag)
#if($key <= 5)
<span>{{$tag}}</span>
#endif
#endforeach
</div>
This will help you.
<div class="tag-area">
#foreach(explode(',',$product->tags) as $key => $tag)
#if($key <= 5)
<span>{{$tag}}</span>
#endif
#endforeach
</div>
If your key is numenric and its of indexed array you can directly do it like:
<div class="tag-area">
#foreach(explode(',',$product->tags) as $key => $tag)
#if($key <= 5)
<span>{{$tag}}</span>
#else
<?php break; ?>
#endif
#endforeach
OR try this;
<div class="tag-area">
<?php $cnt == 0; ?>
#foreach(explode(',',$product->tags) as $tag)
<span>{{$tag}}</span>
<?php
$cnt++;
if($cnt >= 5)
break;
?>
#endforeach
Remember break; will stop unnecessary execution of loop
if you have 10 element in array no need to iterate after 4 iteration so you should break foreach iteration
<div class="tag-area">
#foreach(explode(',',$product->tags) as $key=>$tag)
#if($key >= 4)
#break
#endif
<span>{{$tag}}</span>
#endforeach
</div>