Laravel Only display first 5 array - php

$reviews has 10 arrays and I am trying to only display the first 5. This is what I have come up so far.
#for ($i = 0; $i < 6; $i++)
#foreach ($reviews as $reviews; i++)
<p>Body</p>
#endforeach
#endfor
Any idea why this isn't working?

Just use the take method and run a foreach loop, like so
#foreach($reviews->take(5) as $review)
<p>{{ $review->body }}</p>
#endforeach
I think that is much simpler and cleaner

Assuming you are using Collections you could use the take method (see here) method:
$collection = collect([0, 1, 2, 3, 4, 5]);
$chunk = $collection->take(3);
$chunk->all();
// [0, 1, 2]
Usage
$reviews2 = $reviews->take(5);
#foreach ($reviews2 as $review)
<p>{{ $review->body }}</p>
#endforeach

As you are asking .
Any idea why this isn't working?
Because you have syntax error here i++
#for ($i = 0; $i < 6; $i++)
#foreach ($reviews as $key=>$values)
<p>{{$i}} : Body</p>
#endforeach
#endfor
Note : changed $reviews row value as $values

You can use #if to limit the loop inside #foreach
#for ($i = 0; $i < 6; $i++)
#foreach ($reviews as $review)
{{$i++}}
#if ($i < 1)
<p>Body</p>
#endif
#endforeach
#endfor

You have written the wrong syntax of foreach
<?php $i = 1; ?>
#foreach ($reviews as $review)
<p>Body</p>
#if ($i == 5)
break;
#endif
<?php $i++; ?>
#endforeach

Related

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.

how to add index in laravel blade html attribute while calling collection

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

How to hide <section> with a child for in this case

I have a loop for in a <section> :
<section>
<h4 role="heading" aria-level="4">#lang('site.scope')</h4>
<ul>
#for( $i = 1; $i <= 4; $i++)
#if( $card->{"gen_champ__q" . $i } == 1 )
<li>#lang( 'questions-general.gen_champ_q' . $i . '_1' )</li>
#endif
#endfor
</ul>
</section>
But I don't have data that meets my condition if (== 1).
So, I would like to hide the complete section.
But I do not know how to do it, do you have an idea? If I go above my section, it will duplicate my content and that's not what I want.
You will write the "header" of the section with the first < li> (and only the first) you have to write. And you will write the "footer" of the section only if any < li> was written.
#for( $i = 1; $i <= 4; $i++)
#if( $card->{"gen_champ__q" . $i } == 1 )
#if (!isset($doSection) && $doSection=true)
<section>
<h4 role="heading" aria-level="4">#lang('site.scope')</h4>
<ul>
#endif
<li>#lang( 'questions-general.gen_champ_q' . $i . '_1' )</li>
#endif
#endfor
#if (isset($doSection))
</ul>
</section>
#endif
EDIT: Just upgraded to avoid unneeded variable initialization.
You can put one extra if before section starts.
#if($i>0)
<section>
<h4 role="heading" aria-level="4">#lang('site.scope')</h4>
<ul>
#for( $i = 1; $i <= 4; $i++)
#if( $card->{"gen_champ__q" . $i } == 1 )
<li>#lang( 'questions-general.gen_champ_q' . $i . '_1' )</li>
#endif
#endfor
</ul>
</section>
#endif
there is many way of doing this one of them is this .
In this when your condition meet then it print else not
#for( $i = 1,$j=1; $i <= 4; $i++)
#if( $card->{"gen_champ__q" . $i } == 1 )
#if($j==1&&$j--)
<section>
<h4 role="heading" aria-level="4">#lang('site.scope')</h4>
<ul>
#endif
<li>#lang( 'questions-general.gen_champ_q' . $i . '_1' )</li>
#endif
#endfor
#if($j==0)
</ul>
</section>
#endif
hope this will help you!
#for( $i = 1; $i <= 4; $i++)
#if( ! empty( $card->{"gen_champ__q" . $i })
&& $card->{"gen_champ__q" . $i } == 1 )
<li>#lang( 'questions-general.gen_champ_q' . $i . '_1' )</li>
#endif
#endfor

How to print only 4 values inside foreach loop?

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>

How can I add operator ternary in class on the view laravel blade?

I try like this :
#for($i = 0; $i < 5; $i++)
...
<div class="image ($i==0) ? 'image-main' : ''">
...
#endfor
But it does not work.
It seems the way of writing is incorrect.
How can I solve this problem?
In laravel's blade file you need to use {{}} to execute php code.
{{ ($i == 0) ? 'image-main' : '' }}
You need to use {{ }}
#for($i = 0; $i < 5; $i++)
...
<div class="image {{ ($i==0) ? 'image-main' : '' }}">
...
#endfor
<div class="{{ ($i == 0) ? 'image-main' : '' }}"></div>
try with this,
#for($i = 0; $i < 5; $i++)
<div class="image{{ ($i==0) ? 'image-main' : '' }}">
#endfor

Categories