How can I add leave counter in laravel? - php

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>

Related

laravel id value changed after adding a nested loop

I used the following code to display the role of the user
however, when I want to edit or delete it leads me to the deleted ids number 2/3 it return the same thing even when I change the for else with foreach Attempt to read property "id" on null (View: C:\laravel\gauto\resources\views\dashboard\user\edit.blade.php)!
when I delete the nested foreach loop everything back to normal
thank you in advance
#forelse($users as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $item->name }}</td>
<td>{{ $item->email }}</td>
<td>
#if($item->email_verified_at)
{{ "verified" }}
#else
{{ "not verified" }}
#endif
</td>
<td>
#foreach($item->roles as $item)
{{ $item['name'] }}
#endforeach
</td>
<td>
Edit
<form action="{{ route('user.destroy', $item->id) }}" method="POST">
#csrf
#method('delete')
<button onclick="return confirm('Are you sure ??')" type="submit">Delete</button>
</form>
</td>
</tr>
#empty
<tr>
blank data
</tr>
#endforelse
Your users are also saved in $item and in the loop you are allocating $item a different value every time.
Try changing the code to
<td>
#foreach($item->roles as $role)
{{ $role['name'] }}
#endforeach
</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:

Counting returned true if statements in Laravel

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>

Laravel 5.2 pagination issue with select box

I would like to display data based on the chosen values from select box and search box. but select box ( maybe search box has same issue too) get reset whenever I click the page 2,3,4....
I believe I missed something with pagination code in the view / controller.
Please let me know the way to solve this issue.
Controller
class MemberListTestController extends Controller
{
public function search(Request $request)
{
$request->flash();
$memberName = $request->input('memberName');
$chosenCenter = $request->input('chosenCenter');
$memberLists= DB::table('tbMbrMember as Member')
->select(['Org.orgNm',
'Member.firstNm',
'Member.lastNm',
'Member.regDate',
'MshipCd.mshipNm',
'Att.dt',
'Member.phoneCell',
'Member.email',
'Group.groupNm',
DB::raw('MAX(Att.dt) AttDt, GREATEST(MAX(Reg.endDt),MAX(Reg.expDt)) rExpDt')])
->join('tbMbrCenter as Center', 'Member.mbrCd', '=', 'Center.mbrCd')
->join('tbCmOrg as Org', 'Center.orgCd', '=', 'Org.orgCd')
->leftjoin('tbMbrGroup as Group','Center.orgCd','=','Group.orgCd')
->leftjoin('tbMbrMshipReg as Reg', 'Member.mbrCd', '=', 'Reg.mbrCd')
->leftjoin('tbCmMshipCd as MshipCd', 'MshipCd.mshipCd', '=', 'Reg.mshipCd')
->leftjoin('tbMbrAtt as Att', 'Att.mbrCd', '=', 'Member.mbrCd')
->where('Center.orgCd', $chosenCenter)
->groupby('Member.mbrCd')
->paginate(25);
$centerLists = DB::table('tbMbrCenter as Center')
->select('Org.orgNm','Center.orgCd')
->join('tbCmOrg as Org', 'Center.orgCd', '=', 'Org.orgCd')
->where('Center.isShow','1')
->where('Org.companyCd','c1')
->where('Org.isShowList','1')
->groupby('Center.orgCd')
->orderby('Org.orgNm')
->get();
return view('member.memberList_test')
-> with('memberLists', $memberLists)
-> with('centerLists', $centerLists);
}
}
Routes
Route::group(['middleware' => ['web']], function () {
Route::get('member/test','MemberListTestController#search');
Route::post('member/test','MemberListTestController#search');
});
View
#extends('layouts.master')
#section('content')
<div class="container-fluid">
<p></p>
<div class="row"></div>
<div class="row">
<div class="col-md-4">
<form method="post" action="{{ url('member/test') }}" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<select class="form-control" id="chosenCenter" name="chosenCenter">
<option value="" selected disabled> Choose Center</option>
#foreach ( $centerLists as $centerList )
<option value= {{ $centerList->orgCd }} #if (old('chosenCenter') == $centerList->orgCd) selected #endif > {{ $centerList->orgNm }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control"
placeholder="Search for First Name, Last Name... 'search all' for blank "
name="memberName">
<span class="input-group-btn">
<button class="btn btn-default " type="submit">Search</button>
</span>
</div>
</div>
</form>
</div>
</div>
<div class='row'></div>
<div class="row">
<table class="table table-striped table-condensed">
<thead>
<th>#</th>
<th>Center</th>
<th>First Name</th>
<th>Last Name</th>
<th>Date of Reg.</th>
<th>Recent Membership</th>
<th>Date of Membership Exp.</th>
<th>Latest Attendance</th>
<th>Cell Phone</th>
<th>Email</th>
<th>Member Group</th>
</thead>
<tbody>
<?php $count = 1; ?>
#foreach($memberLists as $memberList)
<tr>
<td>{{ (($memberLists->currentPage() - 1 ) * $memberLists->perPage() ) + $count++}}</td>
<td> {{ $memberList->orgNm }}</td>
<td> {{ $memberList->firstNm }} </td>
<td> {{ $memberList->lastNm }} </td>
<td> {{ $memberList->regDate }} </td>
<td> {{ $memberList->mshipNm }}</td>
#if (($memberList->rExpDt) >= date('y-m-d'))
<td> {{$memberList->rExpDt }}</td>
#else
<td> Expired on {{ $memberList->rExpDt }} </td>
#endif
<td>{{ $memberList->AttDt }}</td>
<td> {{ $memberList->phoneCell }}</td>
<td> {{ $memberList->email }}</td>
<td> {{ $memberList->groupNm }} </td>
</tr>
#endforeach
</tbody>
</table>
{!! $memberLists->render() !!}
</div>
</div>
#stop
You have to pass the parameters via GET and recover the parameters from the URL.
{!! $memberLists->appends(Input::all())->render() !!}
This will send you to page 2 and so on... with all the inputs values.
I found a video in laracasts that explains all the built-in pagination options for your blade template in laravel 5.2 :
https://laracasts.com/lessons/crazy-simple-pagination
you will see that the correct way for a search page is:
{{ $memberLists->appends(Request::except('page'))->links() }}
This way it will keep the current url parameteres.

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