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
Related
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
I have a "teachers" table and a "courses" table. I get to access my "courses" data when I'm on a "teachers" view with a foreach.
#foreach($teacher->courses as $course)
<li>{{ $course->title }}</li>
#endforeach
But I would like to hide the section if it "teachers" has no "course".
#if(teacher_has_course)
<section class="My section">
#foreach($teacher->courses as $course)
<li>{{ $course->title }}</li>
#endforeach
</section>
#endif
I do not understand the principle. Can you help me ? Thank you
you can check like this:
#if(!empty($teacher->courses) && count($teacher->courses))
<section class="My section">
#foreach($teacher->courses as $course)
<li>{{ $course->title }}</li>
#endforeach
</section>
#endif
<section class="My section">
#foreach($teacher->courses as $course)
#unless(!$course)
<li>{{ $course->title }}</li>
#endunless
#endforeach
</section>
or
#unless(!$teacher->courses)
<section class="My section">
#foreach($teacher->courses as $course)
<li>{{ $course->title }}</li>
#endforeach
</section>
#endunless
If you have defined a relation between two tables you can use ->has() method. Lets say there is one to many relation between 'teachers' and 'courses' then you can check like this
#if($teacher->has('course'))
// html code that you want to show
#endif
Refer https://laravel.com/docs/5.4/eloquent-relationships#querying-relationship-existence
Hope this helps
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
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
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