How to use foreach inside another foreach in blade laravel - php

from my codes under, i want to set foreach enter another foreach as shown, i have two tables (reservation and courses ), and the objective is to find the status of course who have the same ID in two tables.
Tables:
Courses: (id_course, statutCourses)
reservation: (id_reserv, Location, Destination, Date, Phone, price)
thanks for helping me
#foreach ($reservations as $reservation)
<tr>
<td> {{ $reservation->id_reserv}}</td>
<td> {{ $reservation->Location}}</td>
<td> {{ $reservation->Destination }}</td>
<td> {{ $reservation->Date }}</td>
<td> {{ $reservation->Phone }}</td>
<td> {{ $reservation->price }}</td>
<td>
#foreach($courses as $course)
#if($course->id_course == $reservation->id_reserv)
<table style="margin-bottom: 15px;">
<tr>
<td style="padding-right: 10px;">Statut :</td>
<td>{{$course->statutCourses}}</td>
</tr>
</table>
#endif
#endforeach
</td>
</tr>
#endforeach

Related

How can i add two numbers in foreach blade in laravel and display the total

I need to add two numbers passed by the foreach in the blade view
how can i add this two numbers instead of showing it as individual 333.34 333.34,
I need to show it as 666.68 / 1000 AUD
Here's how blade foreach looks like
#if(!empty($teams)&&count($teams)>0)
#foreach($teams as $key=>$team)
<table>
<tr>
<th>id</th>
<th>Team Name</th>
<th>Payment</th>
<th>Time Remaining</th>
<th>Num of Players Paid</th>
<th>Paid out of</th>
<th>Captain Name</th>
<th>Status</th>
</tr>
<tr>
<td>{{$key+1}}</td>
<td>{{$team->name}}</td>
<td>{{($team->pivot->status==0)?'PENDING':(($team->status==1)?'REGISTERED':(($team->pivot->status==3)?'REMOVED':(($team->pivot->status==2)?'WITHDRAWN':'')))}}</td>
<td>
#if ($team->pivot->status == 0)
{{\Carbon\Carbon::createFromTimeStamp(strtotime($team->pivot->created_at.' +1 day'))->diffForHumans(null, true, true, 2)}}
#else
Registered at {{$team->pivot->updated_at->todatestring()}}
#endif
</td>
<td>{{ $team->competitionPayments->count() . '/' . $team->players->count() .' PAID'}}</td>
<td>
#foreach ($team->competitionPayments as $amount)
{{ $amount->amount }}
#endforeach
/
#foreach ($team->competitions as $total)
{{ $total->pivot->total_fee .' AUD'}}
#endforeach
</td>
<td>{{$team->captain->full_name}}</td>
<td>{{$team->pivot->status}}</td>
{{-- <td>{{($team->pivot->status==0)?'PENDING':(($team->status==1)?'REGISTERED':(($team->pivot->status==3)?'REMOVED':(($team->pivot->status==2)?'WITHDRAWN':'')))}}</td> --}}
</tr>
</table>
<table>
<tr>
<th>Full Name</th>
<th>DOB</th>
<th>Active Kids Voucher AKV</th>
<th>Accept Reject AKV</th>
<th>Paid $</th>
<th>Paid By </th>
<th>Total</th>
<th>Stripe</th>
<th>Mobile</th>
<th>Email</th>
<th>T&C</th>
</tr>
#foreach ($team->players as $player)
<tr>
<td>{{ $player->full_name }}</td>
<td>{{ $player->dob }}</td>
<td>-</td>
<td>-</td>
<td>
{!! $player->competitionPayments->isNotEmpty() ?
implode($player->competitionPayments->map(function($item, $key) {
return ($key > 0 ? '<div class="mb-1"></div>' : '') . number_format($item->amount, 2) . ' AUD <br> <hr>' . $item->updated_at->todatestring();
})->toArray()) : '-' !!}
</td>
<td>{{ $player->competitionPayments->isNotEmpty() ? $player->competitionPayments->implode('paidBy.name') : '-' }}</td>
<td>-</td>
<td>-</td>
<td>{{ $player->mobile }}</td>
<td>{{ $player->email }}</td>
<td>-</td>
</tr>
#endforeach
</table>
<br><br><hr><br><br>
#endforeach
#else
<tr>
<td colspan="6">
<div class="text-muted small text-center">
No teams have registered yet
</div>
</td>
</tr>
#endif
Heres the relationship in my Team model
public function competitionPayments()
{
return $this->hasMany(CompetitionPayment::class, 'team_id');
}
Here's my controller function
public function detailedRegistrations($competition)
{
$competitions = $this->competitionsRepo->findOrFail($competition);
$teams = $competitions->teams()->get();
$payments = $competitions->payments()->get();
return view('competitions.detailed-registrations',
[
'teams'=>$teams,
'payments'=>$payments,
'competitions'=>$competitions,
]
);
}
I tried to add the sum() and get the total as below and i got a wrong total and repeated
<td>
#foreach ($team->competitionPayments as $amount)
{{ $amount->sum('amount') }}
#endforeach
/
#foreach ($team->competitions as $total)
{{ $total->pivot->total_fee .' AUD'}}
#endforeach
</td>
This is the total i got
Someone please help me out or suggest a way that i could fix this issue
Thank You
Try array_sum() method. Replace this block
#foreach ($team->competitionPayments as $amount)
{{ $amount->amount }}
#endforeach
With this
#php
$temparr = (array) $team->competitionPayments
#endphp
{{array_sum(array_column($temparr,'amount'))}}
Or directly try
#php
$sum = 0
#endphp
#foreach ($team->competitionPayments as $amount)
$sum = $sum + $amount->amount
#endforeach

Laravel - Illegal string offset

I try to color table cell in laravel based on cell content but i keep getting this error:
"Illegal string offset 'Disponible' (View:
C:\Users\RAYLAN\Documents\CRMSAV\resources\views\
pagination_data.blade.php)
(View: C:\Users\RAYLAN\Documents\CRMSAV\re..."
This is my code:
#foreach($data as $row)
<tr>
<td>{{ $row->ID_Piece }}</td>
<td>{{ $row->Designation }}</td>
<td style="background-color: {{ $row->Status['Disponible'] }}">
{{ $row->Status }}
</td>
</tr>
{{$row->Status = array('Disponible' => '#FF0', 'N' => '#F0F')}}
#endforeach
<tr>
<td colspan="3" align="center">
{!! $data->links() !!}
</td>
</tr>
It's saying that there is no Disponible in this line:
{{$row->Status = array('Disponible' => '#FF0', 'N' => '#F0F')}}
Perhaps write this on the line above and check what is in $row->Status:
<?php
dd( $row->Status );
?>
But it looks a bit wierd, to be honest. Remember that the double-mustaches ( {{ $foobar }} ) are echoing the content. But you're assigning a value in there... To something that you're looping through?!
if your $row->Status['Disponsible'] is present in all the rows then try the below
<td style="background-color: {{ $row->Status['Disponible'] }}">
{{ $row->Status['Disponsible'] }}
</td>
EDIT:
try to substitute for your #foreachloop
#foreach($data as $row)
{{$row->Status = array('Disponible' => '#FF0', 'N' => '#F0F')}}
<tr>
<td>{{ $row->ID_Piece }}</td>
<td>{{ $row->Designation }}</td>
<td style="background-color: {{ $row->Status['Disponible'] }}">
{{ $row->Status['Disponible'] }}
</td>
</tr>
#endforeach
I decided to do it with Jquery, here is the working solution :
#foreach($data as $row)
<tr>
<td>{{ $row->ID_Piece }}</td>
<td>{{ $row->Designation }}</td>
<td id="status">{{ $row->Status }}</td>
</tr>
#endforeach
<tr>
<td colspan="3" align="center">
{!! $data->links() !!}
</td>
</tr>
<script type="text/javascript">
$(document).ready(function(){
$('#status').each(function(){
if ($(this).text() == 'N') {
$(this).css('background-color','#f00');
}
});
});
</script>

my pagination laravel is weird( i dont know to tell in english my english is suck)

so I recently learn how to use Laravel for my class project, and heres my problem
when I create pagination the result in my project is <
.1
>
like that, the pagination is not styled and the pagination like the picture I upload, I have done everything I could do , I browse the internet too, and the result is null , thanks heres the picture of my problem
$no = 1 {{-- buat nomor urut --}}
)
#if ($forms->count() > 0)
#foreach ($forms as $angs)
<tr align="center">
<td height="32">{{ $no++ }}<div align="center"></div></td>
<td>{{ $angs->username }}<div align="center"></div></td>
<td>{{ $angs->nama }}<div align="center"></div></td>
<td>{{ $angs->nik}}<div align="center"></div></td>
<td>{{ $angs->status_aktif}}<div align="center"></div></td>
<td bgcolor="#EEF2F7"><div align="center">Detail | Edit | Hapus</div></td>
<tr align="center" bgcolor="#DFE6EF">
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tr>
#endforeach
{{ $forms->links()}}
`
this is my code in blade
and this is my code in my controller
`
$forms= login::paginate(3);
return view('crud.form.viewMember', compact('forms'));
`

Display data on table out of bound using Laravel

So, Basically the Company can have one, two or no director and I want to display based on the company profile.
I want to display table like below (please don't mind the red border, I just want to highlight what I really want to changed in my table):-
and this is what I got:
this is my loop for displaying inside the table:
<table class="table table-borderless table-sm table_view2">
<thead>
<tr class="spaceSmall">
<td style="width:50%;padding-left: 10px;">NOTES TO ACCOUNT<br>Schedules referred to above and notes attached there to form an integral part of Balance Sheet.<br>This is the Balance Sheet referred to in our Report of even date.</td>
<td style="width:25%"></td>
<td style="width:25%"></td>
</tr>
</thead>
<tbody>
<tr class="spaceSmall">
<td style="padding-left: 25px;">{{ $auditor->auditor_name }}<br>{{ $auditor->qualification }}<br>Membership No. : {{ $auditor->membership_number }}</td>
#foreach($tasks->work->company->directors as $director)
#if($director->pivot->director_type == 'Both' || $director->pivot->director_type == 'Director')
#if(count($director) == 1)
<td></td>
<td>{{ $director->salutation }} {{ $director->first_name }} {{ $director->last_name }}<br>DIRECTOR<br>{{ $director->din }}</td>
#elseif(count($director) == 2)
<td>{{ $director->salutation }} {{ $director->first_name }} {{ $director->last_name }}<br>DIRECTOR<br>{{ $director->din }}</td>
#else
<td></td>
<td></td>
#endif
#else
<td></td>
<td></td>
#endif
#endforeach
</tr>
</tbody>
</table>
If company doesn't have director or if company has 1 director, then it is working perfectly, but if company has 2 directors then it try to insert at the 3rd and 4th column but I don't have that 4th column, If I removed the if condition inside that foreach then it's displaying like the 1st image but I have to check the director(s) they had.
Thanks in Advance
I got the answer, for anyone seeking the answer I just put this line outside the loop then everything works fine.
#php $countDirector = count($tasks->work->company->directors); #endphp
And inside my foreach
#if($countDirector == 1)
<td></td>
<td>{{ $director->salutation }} {{ $director->first_name }} {{ $director->last_name }}<br>DIRECTOR<br>{{ $director->din }}</td>
#elseif($countDirector == 2)
<td>{{ $director->salutation }} {{ $director->first_name }} {{ $director->last_name }}<br>DIRECTOR<br>{{ $director->din }}</td>
#else
<td></td>
<td></td>
#endif

foreach loop executing one by one in laravel

I have a view in which I have to execute foreach loops. The problem is that I want to get one input from first foreach then get the other input from the second foreach. I have an outer for loop which will execute until all the foreach is exhausted.
#for($i=0;$i<=$n;$i++)
<tr>
<th scope="row">{{$i}}</th>
#foreach($Names as $Name)&&
<td>{{$Name}}</td>
#endforeach
#foreach($users as $user)
<td>{{$user}}</td>
#endforeach
#endfor
try like this
#foreach($Names as $key => $val)&&
<td> {{$val}}</td>
<td> {{$users[$key]}} </td>
#endforeach
Your making a mistake with your structure here. You should have the Users and there names and username in one variable so you can foreach this result.
#foreach($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->user }}</td>
</tr>
#endforeach
If you want to add numbers to the list just add a counter with css or php if thats your way.
<?php $i=1; ?>
#foreach($users as $user)
<tr>
<td>{{ $i }}
<td>{{ $user->name }}</td>
<td>{{ $user->user }}</td>
</tr>
<?php $i++; ?>
#endforeach
thanks for trying guys. I got the answer...
#foreach($Names as $Name)
<tr>
<th scope="row">{{$i++}}</th>
{{--#foreach(#Names as $Name)--}}
<th> {{$Name}}</th>
#foreach($users as $user )
<td> {!! Form::text($users.$i,null, array('class' => 'form-control','id'=>$i)) !!} </td>
#endforeach
</tr>
#endforeach
The catch was that for loop and foreach required the same number of iterations. So we can simply eliminate for loop.

Categories