Laravel 4 - foreach in foreach - php

I'm displaying some content with a #foreach. But I want to display that content in a different way if this data match with any data from another #foreach...
This is my code:
#foreach($todasactividades as $a)
<div class="marco-cabecera">
#foreach($actividades as $activ)
#if($a->id == $activ->id)
<?php $bandera = true; ?>
#elseif($a->id != $activ->id)
<?php $bandera = false; ?>
#if($bandera)
{{ $a->actividad }}
#elseif(!$bandera)
{{ $a->actividad }} Something different!!
#endif
#endif
#endforeach
#endforeach
I have tried some different things, so now I think my code is a mess...
Does anybody know how can I fix it?
Thanks in advance!

I have solved this situation in this way...
#foreach($todasactividades as $a)
<?php $bandera = 0 ?>
#foreach($actividades as $activ)
#if($a->id == $activ->id)
<?php $bandera++ ?>
#endif
#endforeach
#if($bandera == 0)
<div class="marco-fondo-blanco">
{{ $a->actividad }}
</div>
#else
<div class="marco-fondo-gris">
{{ $a->actividad }}
</div>
#endif
#endforeach

This cleans up your code and fixes some of the logic issues.
#foreach($todasactividades as $a)
<div class="marco-cabecera">
#foreach($actividades as $activ)
#if($a->id == $activ->id)
{{ $a->actividad }}
#else
{{ $a->actividad }} Something different!!
#endif
#endforeach
</div>
#endforeach

Don't do these loops inside eachother. Make a lookup array
$activities = array();
#foreach($todasactividades as $a) {
$activities[$a->id] = $a->id;
}
Then make your primary loop and check to see if your activity is set
<div class="marco-cabecera">
#foreach($actividades as $activ)
#if(isset($activities[$activ->id]))
{{ $a->actividad }}
#else
{{ $a->actividad }} Something different!!
#endif
#endforeach
</div>

You may try following instead, no need to use #if and <?php tag or other variable for this):
#foreach($todasactividades as $a)
<div class="marco-cabecera">
#foreach($actividades as $activ)
{{ $a->actividad }}
{{ $a->id != $activ->id ? 'Something different!!' : '' }}
#endforeach
</div>
#endforeach

Related

How can I add leave counter in laravel?

I have 2 columns medical and casual leave, I have 2 medical leaves in my database , So i want count of that to leaves, instead of that it displays me 1 1, How can I resolve that?
here is my code of view file
<td>
#foreach($employeeLeave as $empleave)
#if($empName['personal_detail']['first_name'] == $empleave->name)
#if($empleave->type == 'casual')
{{ count([$empleave->type]) }}
#endif
#endif
#endforeach
</td>
<td>
#foreach($employeeLeave as $empleave)
#if($empName['personal_detail']['first_name'] == $empleave->name)
#if($empleave->type == 'medical')
{{ count([$empleave->type]) }}
#endif
#endif
#endforeach
</td>
You can do something like counting in loops at first and then populating
#php
$casualCount = $medicalCount = 0;
#endphp
#foreach($employeeLeave as $empleave)
#if($empName['personal_detail']['first_name'] == $empleave->name)
#if($empleave->type == 'casual')
#php $casualCount++; #endphp
#endif
#if($empleave->type == 'medical')
#php $medicalCount++; #endphp
#endif
#endif
#endforeach
And then your html
<td>
{{ $casualCount }}
</td>
<td>
{{ $medicalCount }}
</td>

need to get next Task in foreach

My For each return the task same as in URL Now I need to get the next task
#foreach ($Tasks as $Task)
#if ($Task->slug == Request::segment(5))
<h2>{{ $Task->task_name }}</h2>
#endif
#if ($loop->remaining)
<p>Next Task is: {{ $Tasks[$loop->iteration]->task_name }}</p>
#endif
#endforeach
I get the task in URL so I need to get next task in foreach but it returns empty
and tried
#foreach ($ATask as $key => $Task)
#if ($ATask[$key]->slug == Request::segment(5))
<h2>{{ $Task->task_name }}</h2>
<p>Next Task is: {{ $ATask[$key++]->task_name }}</p>
#endif
#endforeach
Maybe this script can slove your problem:
#foreach ($Tasks as $key => $Task)
#if ($Task->slug == Request::segment(5))
<h2>{{ $Task->task_name }}</h2>
<p>Next Task is: {{ $Tasks[$key+1]->task_name }}</p>
#endif
#endforeach
Hope it helps.
Also you can use $key++ instead of $key+1

Laravel foreach each item increment by 3

I don't really know the title of this one and tried searching but I don't have any idea on what keywords should I put it on so I create a new one but if there is already a similar post to this. Can you link it? My question is how do I achieve this goal in the image below.
#php $i = 0; #endphp
#foreach ($posts as $key => $post)
#if ($key % 2 === 0)
<div class="row">
<div class="col-md-4"><img src="{{ $post->image }}" /></div>
<div class="col-md-8">
- {{ $i+=1 }}
- {{ $i+=2 }}
- {{ $i+=3 }}
</div>
</div>
#else
#endif
#endforeach
The alternate class from bootstrap is working but the numbers that would increment on each value is I don't have any idea on how to get it done.
I'm not familiar with Larval syntax, but for the logic I think you should have something more like this:
#php $i = 0; #endphp
#foreach ($posts as $key => $post)
#if ($key % 2 === 0)
<div class="row">
<div class="col-md-4"><img src="{{ $post->image }}" /></div>
<div class="col-md-8">
- {{ ++$i }}
- {{ ++$i }}
- {{ ++$i }}
</div>
</div>
#else
<div class="row">
<div class="col-md-8">
- {{ ++$i }}
- {{ ++$i }}
- {{ ++$i }}
</div>
<div class="col-md-4"><img src="{{ $post->image }}" /></div>
</div>
#endif
#endforeach
Things to note that I changed:
1. Increment $i by one for EACH number, otherwise you'll be skipping numbers
2. When $key is NOT event, print the image on the other side.
Again, I'm not familiar with larval syntax, and I also don't know what the key/values of your $posts array are (if your keys aren't alternating even/odd you won't have the images back-and-forth between left-and-right).
Hopefully this gives you some direction though.

Echoing data if it exists

I want to display default data if the table is empty. I searched in the laravel documentation and found this.
In my controller i send data to my show.blade.php
public function show($id)
{
//Get profile data from the user
$user = User::find($id);
return view( 'gebruiker/show',[
'user' => $user,
] );
}
And in my blade I display the data
#foreach ($user->profile as $profile)
<div class="col-md-12 col-xs-12" align="center">
<h1>{{ $profile->user->name or 'Geen naam' }}
</h1>
<h3>{{ $profile->age or 'Geen leeftijd' }}</h3>
<span>{{ $profile->country or 'Geen land' }}</span>
</div>
<div class="col-md-6 col-xs-6 follow line" align="center">
<h3>
Bezoekers <br/> <span>{{ $profile->visitors or 'Geen bezoekers' }}</span>
</h3>
</div>
<div class="col-md-12 col-xs-12 login_control text-center">
<br>
{{ $profile->profile or 'Geen beschrijving' }}
</div>
#endforeach
When $profile->age is not empty it shows data but when there is no data it just shows nothing. What am I doing wrong?
In blade "or" checks only if variable exists.
Lest try something like that:
#if(!empty($profile->age))
{{ $profile->age }}
#else
Geen leeftijd
#endif
You can also try this:
#if($profile->age !='' && $profile->age !='0')
{{ $profile->age }}
#else
Geen leeftijd
#endif
Depending on your needs you can create an accessor in your model:
public function getAgeAttribute($value)
{
return !empty($value) ? $value : 'Geen leeftijd';
}
Ideally this should live in a presenter. Too bad {{ $profile->age or 'Geen leeftijd' }} doesn't work, it should. Probably someone will do a PR for it soon enough.

How can i have the form text empty?

I dont know the way to leave the the form text empty is there is no result.I need something like this.....
If there is no userCreator = form text empty
My code
<div class="col-md-4">
<div class="form-group">
{{ Form::label('userCreator','UserCreator') }}
#if(isset($userCreator))
#foreach($userCreator as $cs)
{{ Form::text('userCreator',$cs->home_lastname,['class'=>'form-control']) }}
#endforeach
#else{ {{-- What should i put inside here ? --}}
}
#endif
</div>
</div>
#if(isset($userCreator) && count($userCreator) > 0)
<div class="col-md-4">
<div class="form-group">
{{ Form::label('userCreator','UserCreator') }}
#foreach($userCreator as $cs)
{{ Form::text('userCreator',$cs->home_lastname,['class'=>'form-control']) }}
#endforeach
</div>
</div>
#endif
try this ...
<div class="col-md-4">
<div class="form-group">
{{ Form::label('userCreator','UserCreator') }}
#if(!empty($userCreator))
#foreach($userCreator as $cs)
{{ Form::text('userCreator',$cs->home_lastname,['class'=>'form-control']) }}
#endforeach
#else
{{ Form::text('userCreator','', ['class'=>'form-control']) }}
#endif
</div>
</div>

Categories