Condition in 75 times loop takes 17 sec in local - php

In a list of user, I have an avatar. When I write this:
<img src="{{ $user->avatar }}" class="img-circle img-sm"/></a>
Scripts take 17 sec for 75 users.
But when I write:
<img src="{{ "/images/avatar/avatar.png" }}" class="img-circle img-sm"/></a>
it goes pretty quick.
Why is this attribute so time consuming???
In this page, I call a lot of attribute from this object, but only this one is time consuming... It's just string more....
I'm in Laravel 5.3
EDIT: Paste full table:
<table class="table datatable-responsive" id="table{{ $championship->id }}">
<thead>
<tr>
<th class="min-tablet text-center "
data-hide="phone">{{ trans('core.avatar') }}</th>
<th class="all">{{ trans('core.username') }}</th>
<th class="phone">{{ trans('core.email') }}</th>
<th align="center" class="phone">{{ trans_choice('categories.category',1) }}</th>
<th align="center" class="phone">{{ trans('core.paid') }}</th>
<th class="phone">{{ trans('core.country') }}</th>
#can('edit',$tournament)
<th class="all">{{ trans('core.action') }}</th>
#endcan
</tr>
</thead>
#foreach($championship->users as $user)
<?php
$arr_country = $countries->where('id', $user->country_id)->all();
$country = array_first($arr_country, null);
?>
<tr>
<td class="text-center">
{{--<a href="{!! URL::action('UserController#show', $user->slug) !!}"><img--}}
{{--src="{{ is_null($user->avatar) ? "/images/avatar/avatar.png" : $user->avatar }}" class="img-circle img-sm"/></a>--}}
</td>
<td>
#can('edit',$user)
{{ $user->name }}
#else
{{ $user->name }}
#endcan
</td>
<td>{{ $user->email }}</td>
<td class="text-center">{{ $championship->category->buildName($grades)}}</td>
<td class="text-center">
#if ($user->pivot->confirmed)
<?php $class = "glyphicon glyphicon-ok-sign text-success";?>
#else
<?php $class = "glyphicon glyphicon-remove-sign text-danger ";?>
#endif
#can('edit',$tournament)
{!! Form::open(['method' => 'PUT', 'id' => 'formConfirmTCU',
'action' => ['CompetitorController#confirmUser', $tournament->slug, $championship->id,$user->slug ]]) !!}
<button type="submit"
class="btn btn-flat btnConfirmTCU"
id="confirm_{!! $tournament->slug !!}_{!! $championship->id !!}_{!! $user->slug !!}"
data-tournament="{!! $tournament->slug !!}"
data-category="{!! $championship->id !!}"
data-user="{!! $user->slug !!}">
<i class="{!! $class !!} "></i>
</button>
{!! Form::close() !!}
#else
<i class="{!! $class !!} "></i>
#endcan
</td>
<td class="text-center"><img src="/images/flags/{{ $country->flag }}"
alt="{{ $country->name }}"/></td>
#can('edit',$tournament)
<td class="text-center">
{!! Form::model(null, ['method' => 'DELETE', 'id' => 'formDeleteTCU',
'action' => ['CompetitorController#deleteUser', $tournament->slug, $championship->id,$user->slug ]]) !!}
<button type="submit"
class="btn text-warning-600 btn-flat btnDeleteTCU"
id="delete_{!! $tournament->slug !!}_{!! $championship->id !!}_{!! $user->slug !!}"
data-tournament="{!! $tournament->slug !!}"
data-category="{!! $championship->id !!}"
data-user="{!! $user->slug !!}">
<i class="glyphicon glyphicon-remove"></i>
</button>
{!! Form::close() !!}
</td>
#endcan
</tr>
#endforeach
</table>

Is the avatar property a relationship to another table? It could be a lazy loading problem inside the foreach.
You can find more info about lazy and eager loading at Eloquent documentation.

Related

Give role acess in php?

Is it right approach to write Laravel PHP code to give a role to hr to view certain column? Because after hosting the website stops automatically and start automatically. Code is working fine..
I am new to PHP, I have no idea if i am going wrong in this code can anyone help me out yrr...
<table class="table table-striped mb-0 dataTable">
<thead>
<tr>
#role('company')
<th>{{__('Employee Name')}}</th>
#endrole
#role('hr')
<th>{{__('Employee Name')}}</th>
#endrole
<th>{{__('Designation')}}</th>
<th>{{__('Promotion Title')}}</th>
<th>{{__('Promotion Date')}}</th>
<th>{{__('Description')}}</th>
#if(Gate::check('Edit Promotion') || Gate::check('Delete Promotion'))
<th width="200px">{{__('Action')}}</th>
#endif
</tr>
</thead>
<tbody class="font-style">
#foreach ($promotions as $promotion)
<tr>
#role('company')
<td>{{ !empty($promotion->employee())?$promotion->employee()->name:'' }}</td>
#endrole
#role('hr')
<td>{{ !empty($promotion->employee())?$promotion->employee()->name:'' }}</td>
#endrole
<td>{{ !empty($promotion->designation())?$promotion->designation()->name:'' }}</td>
<td>{{ $promotion->promotion_title }}</td>
<td>{{ \Auth::user()->dateFormat($promotion->promotion_date) }}</td>
<td>{{ $promotion->description }}</td>
#if(Gate::check('Edit Promotion') || Gate::check('Delete Promotion'))
<td>
#can('Edit Promotion')
<i class="fas fa-pencil-alt"></i>
#endcan
#can('Delete Promotion')
<i class="fas fa-trash"></i>
{!! Form::open(['method' => 'DELETE', 'route' => ['promotion.destroy', $promotion->id],'id'=>'delete-form-'.$promotion->id]) !!}
{!! Form::close() !!}
#endif
</td>
#endif
</tr>
#endforeach
</tbody>
</table>
You can find what you are asking in the documentation here (version select on the left):
https://spatie.be/docs/laravel-permission/v5/basic-usage/blade-directives
#role('hr')
I am from hr!
#else
I am not from hr...
#endrole
It further reads #hasrole('hr') is an alias of #role('hr'), so no worries there.
Similarly, the same goes for permissions:
#can('edit articles')
//
#endcan
For more information, see link mentioned above.

Popup box and content on each table row

Original code
<tbody id="table">
#foreach($services as $service)
<tr>
<td colspan="7" class="theme-bg" style="color:white">{{ $service->name }}
#if($service->brand != '')
<span style="font-family:'Font Awesome 5 Brands','FontAwesome';">&#x{{ $service->brand }};</span>
#endif
</td>
</tr>
<tr>
<th>#lang('general.package_id')</th>
<th>#lang('general.name')</th>
<th>#lang('general.price_per_item') {{ getOption('display_price_per') }}</th>
<th>#lang('general.minimum_quantity')</th>
<th>#lang('general.maximum_quantity')</th>
<th>#lang('general.description')</th>
</tr>
#foreach($packages as $package)
#if(isset($categories[$service->id]) && in_array($package->category_id,explode(',',$categories[$service->id])))
<tr>
<td>{{ $package->id }}</td>
<td>{{ $package->name }}</td>
<td>
#php
$price = isset($userPackagePrices[$package->id]) ? $userPackagePrices[$package->id] : $package->price_per_item;
#endphp
{{ getOption('currency_symbol') . number_format(($price * getOption('display_price_per')),2, getOption('currency_separator'), '') }}
</td>
<td>{{ $package->minimum_quantity }}</td>
<td>{{ $package->maximum_quantity }}</td>
<td style="white-space: pre-line">{{ $package->description }}</td>
</tr>
#endif
#endforeach
#endforeach
</tbody>
I was trying to replace "{{ $package->description }}" with a button to show the content in a new popup, using this.
<tbody id="table">
#foreach($services as $service)
<tr>
<td colspan="7" class="theme-bg" style="color:white">{{ $service->name }}
#if($service->brand != '')
<span style="font-family:'Font Awesome 5 Brands','FontAwesome';">&#x{{ $service->brand }};</span>
#endif
</td>
</tr>
<tr>
<th>#lang('general.package_id')</th>
<th>#lang('general.name')</th>
<th>#lang('general.price_per_item') {{ getOption('display_price_per') }}</th>
<th>#lang('general.minimum_quantity')</th>
<th>#lang('general.maximum_quantity')</th>
<th>#lang('general.description')</th>
</tr>
#foreach($packages as $package)
#if(isset($categories[$service->id]) && in_array($package->category_id,explode(',',$categories[$service->id])))
<tr>
<td>{{ $package->id }}</td>
<td>{{ $package->name }}</td>
<td>
#php
$price = isset($userPackagePrices[$package->id]) ? $userPackagePrices[$package->id] : $package->price_per_item;
#endphp
{{ getOption('currency_symbol') . number_format(($price * getOption('display_price_per')),2, getOption('currency_separator'), '') }}
</td>
<td>{{ $package->minimum_quantity }}</td>
<td>{{ $package->maximum_quantity }}</td>
<td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter">+ Description</button></td>
</tr>
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p style="white-space: pre-line">{{ $package->description }}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
#endif
#endforeach
#endforeach
</tbody>
But I'm getting same content on each button, but the content must be different because each row have won service name + description. This description must be different on the popup box.
The problem might be caused by your modal-ids. Since all your modals have the id "exampleModalCenter" your Button probably only opens the first one it can find (since ids are supposed to be unique).
If you would dynamicly change your modal-ID and the corresponding Button-target
Example with adding your package-ID:
<tbody id="table">
#foreach($services as $service)
<tr>
<td colspan="7" class="theme-bg" style="color:white">{{ $service->name }}
#if($service->brand != '')
<span style="font-family:'Font Awesome 5 Brands','FontAwesome';">&#x{{ $service->brand }};</span>
#endif
</td>
</tr>
<tr>
<th>#lang('general.package_id')</th>
<th>#lang('general.name')</th>
<th>#lang('general.price_per_item') {{ getOption('display_price_per') }}</th>
<th>#lang('general.minimum_quantity')</th>
<th>#lang('general.maximum_quantity')</th>
<th>#lang('general.description')</th>
</tr>
#foreach($packages as $package)
#if(isset($categories[$service->id]) && in_array($package->category_id,explode(',',$categories[$service->id])))
<tr>
<td>{{ $package->id }}</td>
<td>{{ $package->name }}</td>
<td>
#php
$price = isset($userPackagePrices[$package->id]) ? $userPackagePrices[$package->id] : $package->price_per_item;
#endphp
{{ getOption('currency_symbol') . number_format(($price * getOption('display_price_per')),2, getOption('currency_separator'), '') }}
</td>
<td>{{ $package->minimum_quantity }}</td>
<td>{{ $package->maximum_quantity }}</td>
<td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter-{{ $package->id }}">+ Description</button></td>
</tr>
<div class="modal fade" id="exampleModalCenter-{{ $package->id }}" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p style="white-space: pre-line">{{ $package->description }}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
#endif
#endforeach
#endforeach
</tbody>

"Class App\Http\Controllers\Frontend\FrontendController does not exist"

I installed a laravel script. but a page not worked and appear
Trying to get property 'name' of non-object (View:
/mylocal/core/resources/views/admin/users/servicePrice.blade.php)
for this line:
<th>{{ $item->service->name }}</th>
this is the file codes:
<div class="tile">
#include('admin.layouts.flash')
<h3 class="tile-title">Service Price Details</h3>
<form method="post" action="{{ route('store.service.price', $user_id) }}">
#csrf
#method('PUT')
<table class="table table-hover">
<tbody>
#foreach($items as $item)
<tr>
<th>{{ $item->category->name }}</th>
<th>{{ $item->service->name }}</th>
<td>
<div class="input-group">
<input type="hidden" name="id[]" value="{{ $item->id }}">
<input class="form-control" type="text" name="price[]" value="{{ $item->price }}">
<div class="input-group-append">
<span class="input-group-text">{{ $general->currency_symbol }}</span>
</div>
</div>
</td>
</tr>
#endforeach
</table>
<div class="tile-footer">
<button class="btn btn-primary btn-block btn-lg" type="submit"><i
class="fa fa-fw fa-lg fa-check-circle"></i>Save
</button>
</div>
</form>
</div>
Check your relation return a object or not.
<th>
#if($item->service)
{{ $item->service->name }}
#endif
</th>
You didn't share the content of array $items.Please make sure you have a service array in $items array and also make sure the name variable exists in service array.
and can you show me the result of this:
var_dump($items);

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:

Submitting more than one record at once - Laravel 5.2

I'm having a table with input fields in every row but i want to submit all of the inputs at once, not one by one. Where should I put the submit button? Like that I'm submitting only the last row in the table.
<table class="table table-responsive table-striped" id="admin-table">
<thead>
<tr>
<th>Клас:</th>
<th>N:</th>
<th>Име:</th>
<th>Предмет:</th>
<th>Оценка:</th>
<th>Тип на оценката:</th>
</tr>
</thead>
<tbody>
#foreach($students as $student)
#foreach($class as $classes)
#foreach($sub as $subject)
<tr>
{!! Form::open(['action' => 'Educator\AccountController#markStudent', $subject, 'class' => 'form-horizontal']) !!}
<td>
{{$classes->name}}
</td>
<td>
{!! Form::text('student_id', $student->id) !!}
</td>
<td>
{{$student->full_name}}
</td>
<td>
{!! Form::text('subject_id', $subject->id) !!}
</td>
<td>
{!! Form::text('mark',null, ['class'=>'form-control col-md-2']) !!}
</td>
<td>
{!! Form::select('markType', $markType, null, ['class'=>'form-control']) !!}
</td>
</tr>
#endforeach
#endforeach
#endforeach
<div align="center">
<button type="button" class="btn btn-default">Назад</button>
{!! Form::submit('Запиши', ['class' => 'btn btn-default']) !!}
</div>
{!! Form::close() !!}
</tbody>
</table>
First off all move Form::open outside of all foreaches.
Then You have to change all you inputs for array (with $i itteration), so your loops will looks like:
{!! Form::open(['action' => 'Educator\AccountController#markStudent', $subject, 'class' => 'form-horizontal']) !!}
<table class="table table-responsive table-striped" id="admin-table">
<thead>
<tr>
<th>Клас:</th>
<th>N:</th>
<th>Име:</th>
<th>Предмет:</th>
<th>Оценка:</th>
<th>Тип на оценката:</th>
</tr>
</thead>
<tbody>
<?php $i = 0; ?>
#foreach($students as $student)
#foreach($class as $classes)
#foreach($sub as $subject)
<td>
{{$classes->name}}
</td>
<td>
{!! Form::text('entry[][student_id]', $student->id) !!}
</td>
<td>
{{$student->full_name}}
</td>
<td>
{!! Form::text('entry[][subject_id]', $subject->id) !!}
</td>
<td>
{!! Form::text('entry[][mark]',null, ['class'=>'form-control col-md-2']) !!}
</td>
<td>
{!! Form::select('entry[][markType]', $markType, null, ['class'=>'form-control']) !!}
</td>
<?php $i++; ?>
#endforeach
#endforeach
#endforeach
</tbody>
</table>
<div align="center">
<button type="button" class="btn btn-default">Назад</button>
{!! Form::submit('Запиши', ['class' => 'btn btn-default']) !!}
</div>
{!! Form::close() !!}
Move your {{ Form::open }} outside foreach just after tbody tag. It should work.

Categories