Counting returned true if statements in Laravel - php

I have a Blade with a rather long (at least for me) set of conditional statements. At the moment it looks like this:
<table>
<tbody>
#foreach($payments as $payment)
<tr>
<td style="width:120px;">{{$payment->id}}</td>
<td>#if($payment->payer_id){{$payment->payer->customer_name}}#endif</td>
<td style="width:120px;">{{$payment->payment_amount}}</td><td>{{$payment->payment_distributions->count()}}
#if($payment->payment_distributions->count()>0)
#foreach($payment->payment_distributions as $pd)
#if($pd->amount > 0)
#if($pd->shipment->balance)
#if($pd->amount < $pd->shipment->balance)
<small class="label pull-right bg-red">1</small>
#else
#endif
#else
#endif
#else
#endif
#endforeach
#else
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
In the middle of all that is where it is important, as you can see, it returns a red 1 if the innermost statement returns true. This is of course solely for my benefit, but what I would like is to have it count how many times within overall if statement it returns true, so rather than returning 7 red 1s, I'd like it to return just a red 7.

Do this:
#php($counter = 0)
#foreach($payment->payment_distributions as $pd)
#if($pd->amount > 0 && $pd->shipment->balance && $pd->amount < $pd->shipment->balance)
#php($counter++)
#endif
#endforeach
<small class="label pull-right bg-red">{{ $counter }}</small>
Instead of this:
#foreach($payment->payment_distributions as $pd)
#if($pd->amount > 0)
#if($pd->shipment->balance)
#if($pd->amount < $pd->shipment->balance)
<small class="label pull-right bg-red">1</small>
#else
#endif
#else
#endif
#else
#endif
#endforeach

#php
$count=0;
#endphp
#foreach($payment->payment_distributions as $pd)
#if($pd->amount > 0)
#if($pd->shipment->balance)
#if($pd->amount < $pd->shipment->balance)
#php
$count++;
#endphp
#else
#endif
#else
#endif
#else
#endif
#endforeach
<small class="label pull-right bg-red">{{$count}}</small>

Do this :
<table>
<tbody>
#foreach($payments as $payment)
#php
$customer_name = ($payment->payer_id) ? $payment->payer->customer_name : "";
$filtered_payment_distributions = $payment->payment_distributions->each(function ($pd, $key) {
if(($pd->amount > 0) && ($pd->shipment->balance) && ($pd->amount < $pd->shipment->balance)){
return $pd;
}
});
#endphp
<tr>
<td style="width:120px;">{{$payment->id}}</td>
<td>{{$customer_name}}</td>
<td style="width:120px;">{{$payment->payment_amount}}</td>
<td>
{{$payment->payment_distributions->count()}}
#if($filtered_payment_distributions->count() > 0)
<small class="label pull-right bg-red">{{$filtered_payment_distributions->count()}}</small>
#endif
</td>
</tr>
#endforeach
</tbody>

Related

Laravel orderBy working while doing dd but not in table while fetching

Into my laravel application while I am fetching data by eager loading order by showing me the correct format of data in dd i.e. the data in DESC format, but while i am compacting data and trying to show it in blade it shows the data in ASC format...
My controller
public function index(Request $request)
{
$data = Rate::with('hospital')->orderBy('id','DESC')->get();
// dd($data );
return view('admin.rates.index',compact('data'));
}
My blade file
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
{{-- <th>No</th> --}}
<th>Hospital</th>
<th>Contract Date</th>
<th class="text-center">Contract Length (weeks)</th>
<th>Weekly</th>
<th>Hourly</th>
<th>Others</th>
{{-- <th>Status</th> --}}
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $i=0; ?>
#foreach ($data as $key => $rate)
<tr>
{{-- <td>{{++$i}}</td> --}}
<td>
{{ ucwords($rate->hospital->short_name) }}
</td>
<td>
{{date('m/d/Y',strtotime($rate->contract_date))}}
</td>
<td class="text-center">
{{ $rate->contract_duration}}
</td>
<td class="text-right">
{{ ($rate->weekly_rate != '') ? $rate->weekly_rate : '' }}
</td>
<td class="text-right">
{{ ($rate->hourly_rate != '') ? $rate->hourly_rate : '' }}
</td>
<td class="text-right">
{{ ($rate->others_rate != '') ? $rate->others_rate : '' }}
</td>
{{-- <td>
</td> --}}
<td>
#if ($rate->status=='inactive')
<i class="fas fa-times"></i>
#else
<i class="fas fa-check"></i>
#endif
<a class="btn btn-sm btn-info" href="{{ route('rate.edit',$rate->id) }}"><i class="fas fa-edit"></i></a>
<a class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete')" href="{{ route('rate.destroy',$rate->id) }}"><i class="fas fa-trash"></i></a>
</td>
</tr>
#endforeach
</tbody>
</table>
There is nothing much more in it but why the data are not showing in DESC format is my question??? I am also pasting my dd image below where you can see the first data has the id 8 and the second one id 7 which is totally correct but while fetching id 7 comes first why??
When you are using datatable it tries to filter or sort the data in nearest alphabetical order if serial number is not in use I saw that you have closed tag for serial no try to open it and check the result. I am quite sure it is due to that... Just try to use "ordering": false into your datatable function..

How to construct a column table using forelse and if in laravel?

this my source code and I apologize for the messy layout of my code syntax, i want to fill the td like this
<tbody>
#forelse ($kaders as $kader)
<tr>
<td>{{ $kader->username }}</td>
#if($kader->status_user == '1')
<td>Admin</td>
#elseif($kader->status_user == '0')
<td>Ibu / Guest</td>
#endif
#forelse ($surveillances as $surveillance)
#if($kader->username == $surveillance->nik_ortu)
#if($surveillance->status == '1')
<td>Aktif</td>
#elseif($surveillance->status == '0')
<td>NonAKtif</td>
#endif
#endif
<!-- <td>admin</td> -->
#empty
<td>admin</td>
#endforelse
<td class="text-center">
<form onsubmit="return confirm('Apakah Anda Yakin ?');" action="{{ route('kader.destroy', $kader->id) }}" method="POST">
#method('DELETE')
#csrf
<a href="{{ route('kader.edit', $kader->id) }}" class="btn btn-sm btn-primary">
<i class="far fa-edit"></i> EDIT
</a>
<button type="submit" class="btn btn-sm btn-danger">
<i class="far fa-trash-alt"></i> HAPUS
</button>
</form>
</td>
</tr>
#empty
<div class="alert alert-danger">
Data Kader belum Tersedia.
</div>
#endforelse
</tbody>
how can i construct like the pic above ? thank you for advice
You need to set the table cell outside the if-then block so if there's nothing an empty cell will be created instead of no cell. Change this:
#if($kader->status_user == '1')
<td>Admin</td>
#elseif($kader->status_user == '0')
<td>Ibu / Guest</td>
#endif
#forelse ($surveillances as $surveillance)
#if($kader->username == $surveillance->nik_ortu)
#if($surveillance->status == '1')
<td>Aktif</td>
#elseif($surveillance->status == '0')
<td>NonAKtif</td>
#endif
#endif
<!-- <td>admin</td> -->
#empty
<td>admin</td>
#endforelse
to this:
<td>
#if($kader->status_user == '1')
Admin
#elseif($kader->status_user == '0')
Ibu / Guest
#endif
</td>
<td>
#forelse ($surveillances as $surveillance)
#if($kader->username == $surveillance->nik_ortu)
#if($surveillance->status == '1')
Aktif
#elseif($surveillance->status == '0')
NonAKtif
#endif
#endif
<!-- <td>admin</td> -->
#empty
admin
#endforelse
</td>
<tbody>
#forelse ($kaders as $kader)
<tr>
<td>{{ $kader->username }}</td>
<td>
#if($kader->status_user == '1')
Admin
#elseif($kader->status_user == '0')
Ibu / Guest
#endif
</td>
<td>
#forelse ($surveillances as $surveillance)
#if($kader->username == $surveillance->nik_ortu)
#if($surveillance->status == '1')
Aktif
#elseif($surveillance->status == '0')
NonAKtif
#endif
#endif
#empty
admin
#endforelse
</td>
<td class="text-center">
<form onsubmit="return confirm('Apakah Anda Yakin ?');" action="{{ route('kader.destroy', $kader->id) }}" method="POST">
<i class="far fa-edit"></i> EDIT
#csrf
#method('DELETE')
<button type="submit" class="btn btn-sm btn-danger"><i class="far fa-trash-alt"></i> HAPUS</button>
</form>
</td>
</tr>
#empty
<div class="alert alert-danger">
Data Kader belum Tersedia.
</div>
#endforelse
</tbody>

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>

Trying to get property of non-object on Laravel 5

Thank you for all the help in the past.
On my app, when i try to view a certain blade, it returns the error
"Trying to get property of non-object".
It has pointed me to the blade and the section of code where the error comes from:
#extends('layouts.app')
#section('content')
#component('dashboard.frame')
<div class="panel panel-default">
<div class="panel-heading">
Create
Activation Pins - {{ $item->name }}
</div>
{{--<div class="panel-body">--}}
{{--#if($pin_item && !$pin_item->pivot->is_paid )--}}
{{--<form action="/activation-pins/{{ $pin_item->id }}" method="post">--}}
{{--<input type="hidden" name="_method" value="PUT">--}}
{{--<input type="hidden" name="vendor_id" value="{{ $item->vendor->id }}">--}}
{{--{!! csrf_field() !!}--}}
{{--<button type="submit" class="btn btn-success">Mark As Paid</button>--}}
{{--</form>--}}
{{--#endif--}}
{{--</div>--}}
<table class="table">
<thead>
<tr>
<th>S/N</th>
<th>Name</th>
<th>Is Active</th>
<th>User</th>
</tr>
</thead>
<tbody>
#forelse($item->activation_pins as $itm)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $itm->pin }}</td>
<td>
<span class="label label-{{ $itm->is_active ? ( $itm->is_valid ? 'info' : 'danger' ) : 'warning' }}">
{{ $itm->is_active ? ( $itm->is_valid ? 'active' : 'expired' ) : 'inactive' }}
</span>
</td>
<td>
{{ $itm->user_id ? $itm->user->email : '-' }}
</td>
</tr>
#empty
<tr>
<td colspan="6">
<p class="text-center">No Activation Pins</p>
</td>
</tr>
#endforelse
</tbody>
</table>
</div>
#endcomponent
#endsection
With emphasis on:
<td>
{{ $itm->user_id ? $itm->user->email : '-' }}
</td>
Could I be missing something? Thanks.
This is what it displays now:

Var emptied from child view to parent view Laravel

I got a really strange behavior in my views. I have a parent view called "access.blade.php" and child view "survey.blade.php".
I instanciated 2 vars in the parent view which are empty arrays.
I populate them in the child view but when i var_dump them in the parent view after the include of the child view, my var are empty...
here is the parent view :
#extends('template')
#section('title')
Compile result
#stop
#section('content')
<div id="access" class="container-fluid main-content">
<div class="row">
<div class="col-xs-6 col-md-push-3 col-xs-push-3">
<div class="page-title white-text text-center">
<h1>Access Report</h1>
</div>
<div class="widget-container fluid-height clearfix">
<?php
$session = Sessions::find($params['session_id']);
$userNotSeen = [];
$userSeen = [];
?>
<div class="heading">
<button class="btn btn-success btn-xl pull-right" role="button" data-toggle="modal" data-target="#complianceModal">Compliance</button>
Access Report for
{{ isset($result->content->display_name) ? $result->content->display_name : $result->content->title }}
<p>
Journey: {{ $session->location_id != null ? $session->location->city : $session->place }}
</p>
</div>
<div class="row text-center"></div>
<div class="row">
<div class="col-sm-offset-1 col-sm-10">
#if($result != null)
#if($result->content_type == 1)
#include('contents.partials.access.tasklist')
#elseif($result->content_type == 2)
#include('contents.partials.access.resources')
#elseif($result->content_type == 3)
#include('contents.partials.access.survey')
#elseif($result->content_type == 4)
#include('contents.partials.access.feedback')
#elseif($result->content_type == 5)
#include('contents.partials.access.memo')
#elseif($result->content_type == 6)
#include('contents.partials.access.gear')
#elseif($result->content_type == 7)
#include('contents.partials.access.sinsim')
#elseif($result->content_type == 8)
#include('contents.partials.access.changr')
#endif
#else
<p class="text-center">{{ $errors }}</p>
#endif
<pre>
{{ var_dump($userNotSeen) }}
</pre>
<pre>
{{ var_dump($userSeen) }}
</pre>
</div>
</div>
</div>
</div>
</div>
</div>
#include('contents.partials.modal')
#stop
And here is my child view
<table class="table">
<thead>
<tr>
<th>Name</th>
<th class="text-center">Viewed</th>
<th class="text-center">Submitted</th>
<th class="text-center">Personnal Results</th>
</tr>
</thead>
<tbody>
#if(isset($result->data))
#forelse($result->data as $key => $value)
<tr {{ $value->view == 0 ? 'class="notyetseen"' : ''; }}>
<td>{{ $value->fullname }}</td>
<td class="text-center">{{ $value->view == 0 ? 'No' : 'Yes' }}</td>
<td class="text-center">{{ $value->done == 0 ? 'No' : 'Yes' }}</td>
<td class="text-center">
Get results
</td>
</tr>
<?php
if($value->view == 0) {
array_push($userNotSeen, $key);
} else {
array_push($userSeen, $key);
}
?>
#empty
<tr><td colspan="3" class="text-center">{{ $errors }}</td></tr>
#endforelse
#else
<tr><td colspan="3" class="text-center">No participants for this journey</td></tr>
#endif
</tbody>
</table>
I have an idea about the why it doesn't work: maybe the parent view is loaded completly before the child view, so my var are not yet populated.
If it is the case, is there any solution?
First of all, never EVER include logic in your templates. Move this logic to your controller and pass results to a view. Templates are only for displaying your data.
If you have to use <?php in your Blade template, you are doing something wrong.
Second, Blade compiles your templates separately. It is not a simple include(__child_template__). That's why while compiling each of your templates, Blade works with completely different variable scopes and $userNotSeen in access.blade.php template is not the same as in your survey.blade.php.

Categories