need to get next Task in foreach - php

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

Related

How to remove repetitive values from foreach loop in php

Greetings I have an array of sections and the question related to that section looks like this
And I loop through that array and output the content to the screen here is the code
#foreach($sections as $section)
<div class="form-group">
<h2>{{ $section['section_name'] }}</h2>
<p>{{ $section['section_description'] }}</p>
<p>{{ $section['section_intro'] }}</p>
</div>
<div class="form-group ms-5">
#foreach($section['question'] as $question)
<h5>{{ $question['question_name'] }}</h5>
<p class="ms-3">{{ $question['question_description'] }}</p>
#endforeach
</div>
#endforeach
And the result of that looks like this
How can I remove this duplicate question_name I just want it to write it once, for example, it writes one Key Contracts and this two question_description under there for all of them?
#foreach($sections as $section)
<div class="form-group">
<h2>{{ $section['section_name'] }}</h2>
<p>{{ $section['section_description'] }}</p>
<p>{{ $section['section_intro'] }}</p>
</div>
<div class="form-group ms-5">
#php
$questions = [];
foreach($section['question'] as $question){
$questions[$question['question_name']][] = $question['question_description'];
}
#endphp
#foreach($questions as $question_name=>$question_descriptions)
<h5>{{ $question_name }}</h5>
#foreach($question_descriptions as $question_description)
<p class="ms-3">{{ $question_description }}</p>
#endforeach
#endforeach
</div>
#endforeach
I haven't tried it but I think it will work.
I'm storing the questions and descriptions in an array where the key is not an index but the name.
You could perhaps map the questions into groups by question name before sending to view. Something like this (for example purposes to get you started):
$sections = collect($sections)->map(function($section, $key){
return [
"section" => $section,
"grouped_questions"=> collect($section["question"])->mapToGroups(function($question, $key){
return [$question['question_name'] => $question];
})->toArray()
];
});
This will give you the questions grouped by their name.

How to group by in laravel with related table?

This is my blade file
<ul>
#foreach ($cajeros as $cajero)
<li>
{{$cajero->idcajero}}
<ul>
#foreach ($cajero->conciliaciones as $conciliacion)
<li>Lote {{ $conciliacion->lote }} - ${{ $conciliacion->importe_comisionable }}</li>
#endforeach
Total cajero: ${{ $cajero->conciliaciones->sum('importe_comisionable') }}
</ul>
</li>
#endforeach
And this is the result
What I want to do is group by lote and get the sum of that.
I tried doing
#foreach ($cajero->conciliaciones->groupby('lote') as $key => $conciliaciones)
<li>Lote {{ $key }} - ${{ sum($conciliaciones) }}</li>
#endforeach
and I got
Call to undefined function sum()
Also, I tried doing it on the controller. But I get the sum as zero.
$detalle = [];
foreach ($cajeros as $cajero){
$detalle[] = [$cajero->id => $cajero->conciliaciones->groupby('lote')->sum('importe_comisionable')];
}
I solved it with
<ul>
#foreach ($cajeros as $cajero)
<li>
{{$cajero->idcajero}}
<ul>
#foreach ($cajero->conciliaciones->groupby('lote') as $key => $conciliaciones)
#php $suma = 0; #endphp
#foreach ($conciliaciones as $conciliacion)
#php $suma += $conciliacion->importe_comisionable; #endphp
#endforeach
<li>Lote {{ $key }} - ${{ $suma }}</li>
#endforeach
Total cajero: ${{ $cajero->conciliaciones->sum('importe_comisionable') }}
</ul>
</li>
#endforeach

Is there a way to spawn 1 item only once inside a foreach loop?

There is one element I want to show only once inside this foreach loop. I was wondering if this was possible or if I would need to change my html/css structure for it with Laravel. I only want to show the icon here once.
example.blade.php :
#foreach ($items as $item)
<p>
{{ $item }}
<i class="fa fa-times><!-- this only once --></i>
</p>
#endforeach
You can do this:
#foreach ($items as $item)
<p>
{{ $item }}
<i class="fa fa-times>
#if(!isset($iconShown))
{{ $iconShown = true }}
DisplayIconHere
#endif
</i>
</p>
#endforeach
Answer from the comments worked best for me :
#foreach ($items as $key => $item)
{{ $item }}
#if($key == 0)
<i class="fa fa-times><!-- this only once --></i>
#endif
#endforeach

Call to a member function count() on a non-object (Laravel 5)

I have a list of projects, where I can click on and it (needs to) show the project name, and the list of the projects' tasks (ToDo application)
But when I click on a certain project I get this error.
(The "H2" project name will show up)
h2>{{{ $project->name }}}</h2>
#if ( !$project->tasks->count())
There are no tasks for this project.
#else
<ul>
#foreach( $project->tasks as $task)
<li>{{ $task->name }}</li>
#endforeach
</ul>
#endif
$project->tasks is an array and you cant call count() on the array. So try with -
count($project->tasks)
Try count($project->tasks)==0
h2>{{{ $project->name }}}</h2>
#if ( count($project->tasks)==0)
There are no tasks for this project.
#else
<ul>
#foreach( $project->tasks as $task)
<li>{{ $task->name }}</li>
#endforeach
</ul>
#endif
I see your issue is resolved but this might help someone else :D
The following piece of code might work for you, but it's not that we can't call count() on the array.
count($project->tasks)
Instead of this
h2>{{{ $project->name }}}</h2>
#if ( !$project->tasks->count()) // tasks is plural -> wrong.
There are no tasks for this project.
#else
<ul>
#foreach( $project->tasks as $task) // here too!
<li>{{ $task->name }}</li>
#endforeach
</ul>
#endif
Try doing this
h2>{{{ $project->name }}}</h2>
#if ( !$project->task->count())
There are no tasks for this project.
#else
<ul>
#foreach( $project->task as $task)
<li>{{ $task->name }}</li>
#endforeach
</ul>
#endif
I use collection for empty objects this could work better
in my controller
if ($search) {
$taks = MODEL::search($search)->get();
} else {
$tasks = collect([]);
}
in my view
#if ($tasks->count())
<ul class="list-group">
#foreach($tasks as $task)
<li>info and action</li>
#endforeach
</ul>
#else
<p class="text-danger">Error message</p>
#endif

Laravel 4 - foreach in foreach

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

Categories