Laravel total foreach index number - laravel

I want to total all the rows to loop, what do I do?
example :
index 0
ixdex 1
I want this ซ
index 2
My code :
#if(isset($unread[$messages_conversation_teachers->conversation_id]))
#foreach($unread[$messages_conversation_teachers->conversation_id] as $_index =>$unreads)
{{$_index}}
#endforeach
#endif

try this $loop->iteration it start from 1 not 0
#if(isset($unread[$messages_conversation_teachers->conversation_id]))
#foreach($unread[$messages_conversation_teachers->conversation_id] as $_index =>$unreads)
{{ $loop->iteration }}
#endforeach
#endif
ref link https://laravel.com/docs/8.x/blade#the-loop-variable

Related

Laravel Sum each section using foreach

I have below table with data.
ID - CLASS - CATEGORY - AMOUNT
--------------------------------
1 I A 5000
2 I B 6000
3 I C 7000
4 V A 9000
5 V B 12000
now I want to sum the amount classwise in blade.php using foreach loop. like class I total is 18000, class V 21000 etc..
Controller Function:
$viewRecords = YearlyFees::Where('registration_id',Auth::user()->id)->get();
return view('yearly_fees_setup',
['viewRecords' => $viewRecords]);
Blade.php
#foreach($viewRecords as $record)
<tr>
<td>{{$record->Standard->class}}</td>
<td>
{{$record->Category->category}}
</td>
<td>{{$record->amount}}
</td>
</tr>
<tr>
<td> ?????? TOTAL of each class </td></tr>
#endforeach
Result:
Class Category Amount
I School Fees 5000
I Tution Fees 6000
I Library Fees 7000
**I WANT TOTAL HERE**
V School Fees 9000
V Tution Fees 12000
**I WANT TOTAL HERE**
Please help
You need to create a variable (or two) inside the for loop do conditionally echo out the total.
#php
$total = 0;
$category = null;
#endphp
#foreach(...)
#php
$total += $record->amount;
#endphp
#if ($category != $record->category)
{{ $total }}
#endif
#endforeach

How to update the text based on the response value in quasar?

I have a q-table where I get a response from API. In one of the entries, it gives the value of the flag as 0 or 1 which I need to update it as Yes or No.
How can I do this?
I am using q-badge to change the bg color and highlight it but unable to update the text.
<template v-slot:body-cell-processed="props">
<q-td :props="props" >
<q-badge style="background-color:white" :class="props.row.processed==0?'bg-red':'text-black'">
{{ props.row.processed }}
</q-badge>
</q-td>
</template>
I have tries this and it's working fine.
<q-td key="processed" :props="props">
<q-badge style="background-color:white" :class="props.row.processed==0?'bg-red text-white text-weight-bolder':'bg-blue text-black'">
{{ props.row.processed }}
</q-badge>
</q-td>
Codepen - https://codepen.io/Pratik__007/pen/PoqzzLx?editable=true&editors=101

Laravel Display Many to Many with Pivot in Blade

Background:
I have a pivot table with Medication, Patient with pivot element day, time, given, given by and lock.
Example:
id medication_id patient_id Day time given
1 1 (MED X) 1 (Patient X) Yesterday 0900 1
2 1 (MED X) 1 (Patient X) Yesterday 1200 1
3 1 (MED X) 1 (Patient X) Today 0900 0
4 2 (MED Y) 1 (Patient X) Tomorrow 1200 0
5 2 (MED Y) 1 (Patient X) Yesterday 0900 1
6 1 (MED X) 2 (Patient Y) Yesterday 1200 1
7 1 (MED X) 2 (Patient Y) Yesterday 0900 1
8 3 (MED Z) 2 (Patient Y) Yesterday 1200 0
A patient can have the same medication but at multiple times within the same day. Let's say Patient X had Med X yesterday at 0900 and 1200, today at 0900 but not tomorrow.
To see all the Medication assigned to the Patient X
$assignedMeds = $patient->medication()->get();
And passed it into the view.
What I have right now is
#if(isset($assignedMeds))
<div class="card-body">
<table class="table table-hover">
<tbody>
<tr>
<th>Name</th>
<th>Yesterday</th>
<th>Today</th>
<th>Tomorrow</th>
</tr>
#foreach($assignedMeds as $assignedMed)
<tr>
<td>{{$assignedMed->name}}</td>
<td>
#if(($assignedMed->pivot->day) == 'yesterday')
#if($assignedMed->pivot->given)
<i class="fas fa-check green"></i>
<strike>{{$assignedMed->pivot->time}}</strike>
<em>{{$assignedMed->pivot->givenby}}</em>
#else
{{$assignedMed->pivot->time}}
#endif
#endif
</td>
<td>
#if(($assignedMed->pivot->day) == 'today')
#if($assignedMed->pivot->given)
<i class="fas fa-check green"></i>
<strike>{{$assignedMed->pivot->time}}</strike>
<em>{{$assignedMed->pivot->givenby}}</em>
#else
<form method="post" action="/mar/{{$assignedMed->id}}">
#csrf
#method('PATCH')
<input hidden name="givenby" id="givenby" value="1">
<button>{{$assignedMed->pivot->time}}</button>
</form>
#endif
#endif
</td>
<td>
#if(($assignedMed->pivot->day) == 'tomorrow')
{{$assignedMed->pivot->time}}
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endif
And it gives me
Medication Name Yesterday Today Tomorrow
Med X 0900 Given
Med X 0900
Med X 1200 Given
Med Y 0900 Given
Med Y 0900
What I am looking for
What I am trying to get is to display the name of medication once and show the time in it.
Example for Patient X
Medication Name Yesterday Today Tomorrow
Med X 0900 Given 0900
1200 Given
Med Y 0900 Given 0900
You could use the groupBy() and where() Collection methods in your #foreach. It should end up looking like this.
<!-- $index will be the 'name' we grouped by (medication name) -->
#foreach($assignedMeds->groupBy('name') as $index => $assignedMed)
<tr>
<!-- medication name -->
<td>{{ $index }}</td>
<!-- yesterday's medications -->
<td>
#foreach($assignedMed->where('pivot.day', 'Yesterday') as $yesterdaysMeds)
#if($yesterdaysMeds->pivot->given)
<i class="fas fa-check green"></i>
<strike>{{$yesterdaysMeds->pivot->time}}</strike>
<em>{{$yesterdaysMeds->pivot->givenby}}</em>
#else
{{$yesterdaysMeds->pivot->time}}
#endif
<!-- line break -->
<br>
#endforeach
</td>
<!-- today's medications -->
<td>
#foreach($assignedMed->where('pivot.day', 'Today') as $todaysMeds)
#if($todaysMeds->pivot->given)
<i class="fas fa-check green"></i>
<strike>{{$todaysMeds->pivot->time}}</strike>
<em>{{$todaysMeds->pivot->givenby}}</em>
#else
<form method="post" action="/mar/{{$todaysMeds->id}}">
#csrf
#method('PATCH')
<input hidden name="givenby" id="givenby" value="1">
<button>{{$todaysMeds->pivot->time}}</button>
</form>
#endif
<!-- line break -->
<br>
#endforeach
</td>
<!-- tomorrow's medications -->
<td>
#foreach($assignedMed->where('pivot.day', 'Tomorrow') as $tomorrowsMeds)
{{ $tomorrowsMeds->pivot->time }}
<!-- line break -->
<br>
#endforeach
</td>
</tr>
#endforeach

laravel Blade is their a neat solution to : Trying to get property of non-object

I have thins line of code
{{($user->company_privileges->level < 3 ? '' : ' disabled') }}
In my menu file
but when a user doesn't have a company I get the error
Trying to get property of non-object
Is their a neat way of dealing with this in one line?
Something like this comes to mind
{{ ($user->company_privileges === null) : '' ? ($user->company_privileges->level < 3 ? '' : ' disabled') ) }}
as the second if statement is only ran if the user has a company
I have the statement inside a tag aswell
<a class="{{($user->company_privileges->level < 3 ? '' : ' disabled') }}">
Why not something like
<a class="{{($user->company_privileges && $user->company_privileges->level < 3 ? '' : ' disabled') }}">
Essentially, what you need here is to know that null == false for bool comparison. Check out the PHP type comparisons table

Using if condition to display data in Blade

How print value in this code:
<li class="price-val">{{ $value->price ? '$value->price € month<span class="price-fea-ct">*VAT Included</span>':'$value->price € month' }}</li>
$value->price variable is have 0 & 9 value respectively.but it does print value.print $value->price just.
Something like this should work
<li class="price-val">
#if ($value->price)
{{{ $value->price }}} € month<span class="price-fea-ct">*VAT Included</span>
#else
{{{ $value->price }}} € month
#endif
</li>
However for me the condition doesn't make sense - if price is 0 you will display 0 / month and it does not seem to be logical
You have syntax errors, try this:
<li class="price-val">
{{
$value->price ?
$value->price . '€ month <span class="price-fea-ct">*VAT Included</span>' :
$value->price . '€ month'
}}
</li>

Resources