how to use 2 foreach in laravel - php

how to use 2 foreach in laravel?
The banner div cannot be included in the forach. Because the layout is broken.
//for show post 1-5
#foreach ($posts as $post)
//.................
#endforeach
//for banner
<div>
...............
</div
//continue foreach (for show 6-...)
#foreach ($posts as $post)
//.................
#endforeach

You can use loop variable, as like :
//for show post 1-5
#foreach ($posts as $post)
#if($loop->index < 5)
// Your code
#endif
#endforeach
//continue foreach (for show 6-...)
#foreach ($posts as $post)
#if($loop->index > 5)
// Your code
#endif
#endforeach

You can break your posts data into 2 chunks in blade
#foreach($posts->chunk(5) as $chunk)
#foreach($chunk as $post)
//.....
#endforeach
//add your banner here after first chunk is rendered
#endforeach
Or a single loop and check for current iteration
#foreach($posts as $post)
//...........
#if($loop->iteration == 5)
//add your banner here
#endif
#endforeach

Related

foreach in foreach in if blade php

#foreach ($capacityGroups as $capacityGroupFather)
#foreach ($exampleProjects as $k => $exampleProject)
#foreach ($exampleProject->capacityGroups as $j => $exampleCapacity)
#if ($exampleCapacity->id == $capacityGroupFather->id)
Only the first result of the loop
<div class="carousel-item active">
The rest of the loop
<div class="carousel-item">
We have the example Projects collection, each of which has capacityGroups.
I need to bring all the example Projects for each capacity Group. But the first of each goes into a special div (carousel-item active)
and the rest of each goes into (carousel-item) how can I fix this problem
Simply use the loop variable.
#foreach ($exampleProjects as $k => $exampleProject)
<div class="carousel-item{{ $loop->first ? ' active' : '' }}">
</div>
#endforeach

setting variables inside a Laravel foreach loop

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
--}}

Hiding results titles if there were no results

I have the following code that shows the output of a search for users, groups and posts :
<header><h1>Users results</h1></header>
#foreach ($users as $user)
#include('user.userblock')
#endforeach
<header><h1>Groups results</h1></header>
#foreach ($groups as $group)
#include('group.gruopblock')
#endforeach
<header><h1>Posts results</h1></header>
#foreach ($posts as $post)
#include('post.postblock')
#endforeach
How can I hide the <header> if one of them doesn't have results ?
Using eloquent #if and #endif with it s count() function:
#if(count($users))
<header><h1>Users results</h1></header>
#foreach ($users as $user)
#include('user.userblock')
#endforeach
#endif
#if(count($groups))
<header><h1>Groups results</h1></header>
#foreach ($groups as $group)
#include('group.gruopblock')
#endforeach
#endif
#if(count($posts))
<header><h1>Posts results</h1></header>
#foreach ($posts as $post)
#include('post.postblock')
#endforeach
#endif
#if (count($users))
<header><h1>Users results</h1></header>
#foreach ($users as $user)
#include('user.userblock')
#endforeach
#endif
and so on...
You've got several choices on how to do it thanks to the nature of the Laravel collection. But an if check conditional on either side will work fine:
if (empty($users)) { // Your header and foreach}
if ($users->first()) { // Your header and foreach}
if (!$users->isEmpty()) { // Your header and foreach}
if ($users->count()) { // Your header and foreach}

How to Count two array in foreach loop

Hi everybody here I have 2 paths so I return 2 arrays of tasks I need to count each array So in last I get
[6, 4]
#foreach ($path->pathtags as $Tag)
#foreach ($Tag->Tasks as $Task)
#if (!in_array($Task->id,$a))
<li class="list-group-item"> Task : {{ $Task->task_name }} </li>
#endif
#endforeach
#endforeach
Easiest php way of counting arrays is count($array) so if you want to make a new array with the 2 array counts you can do it like this:
$counts = [count($array1), count($array2)];
But if you need to count the records in the database, you will need to change the query instead of ->get() you will need to use ->count().
You should try this:
$path = count($path->pathtags);
$task = count($Tag->Tasks);
Updated answer
$path = count($path->pathtags);
#foreach ($path->pathtags as $Tag)
$task = count($Tag->Tasks);
#foreach ($Tag->Tasks as $Task)
<li class="list-group-item"> Task : {{ $Task->task_name }} </li>
#endforeach
#endforeach

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

Categories