Laravel dompdf taking too much time to generate and download a pdf - pdf-generation

I'm recently working on report generation for simple 2 records from database. For generating this report i'm using Laravel dompdf package, but it's taking too long time to download which is not expected. Code I used is below:
$pdf = PDF::loadView('dashboard.sales-report', compact('sales'));
$pdf->setPaper('A4', 'portrait');
$sales_report_file_name = "daily_sales_".date('Y-m-d').".pdf";
return $pdf->download($sales_report_file_name);
and using css inside our view page. But only two records taking more than 2 minutes. Could anyone please help me on it? Thanks in advance.
Blade file code:
#extends('layouts.invoice_master')
#section('content')
#php
$total_discount = 0;
$total_advance = 0;
$total_net_sum = 0;
$total = 0;
#endphp
<!-- START CONTAINER FLUID -->
<div class=" container-fluid container-fixed-lg">
<!-- START card -->
<div class="card card-default m-t-20">
<div class="card-body">
<!-- Define header and footer blocks before your content -->
<header>
<p>{{ $report_for }} </p>
<p>{{ $report_for_date }}</p>
<p>{{ $report_for_month }}</p>
</header>
<div class="invoice padding-50 sm-padding-10" style="padding-left:unset !important;">
<div class="row">
<div class="col-lg-3" style="padding-left:5px !important;">
<img style="width: 200px;margin-bottom: 20px" alt="" class="invoice-logo" src="{{ asset('uploads/company/'.$company_details->id.'/receipt_logo/'.$company_details->receipt_logo)}}">
</div>
<div class="col-lg-8">
<address class="m-t-10">
#if($out_let)
{{ $out_let[0]->name }}
<br>{{ $out_let[0]->location }}
<br>{{ $out_let[0]->address }}
<br>{{ $out_let[0]->city }} , {{ $out_let[0]->zip }}
<br>Contact: {{ $out_let[0]->phone }}
<br>
#endif
</address>
</div>
</div>
<div class="clearfix"></div>
<div class="table-responsive table-invoice">
<table class="table m-t-25">
<thead>
<tr>
<th class="text-center">Date</th>
<th class="text-center">Invoice</th>
<th class="text-center">Customer</th>
<th class="text-center">Total</th>
<th class="text-center">Discount</th>
<th class="text-center">Net.Sum</th>
<th class="text-center">Advance</th>
<th class="text-center">Remain</th>
<th class="text-center">Currency</th>
</tr>
</thead>
<tbody>
#if( $sales )
#foreach( $sales as $sale )
<tr>
<td class="text-center">
{{ $sale->created_at }}
</td>
<td class="text-center">
<p class="text-black">
#if(isset($sale->invoice_prefix_id))
{{
App\InvoicePrefix::find($sale->invoice_prefix_id)->name
}}-{{ $sale->invoice_no
}}
#else
{{ $sale->invoice_no }}
#endif
</p>
</td>
<td class="text-center">
#if(isset($sale->customer_id))
{{ App\Customer::find($sale->customer_id)->first_name }}
{{ App\Customer::find($sale->customer_id)->last_name }}
( {{ App\Customer::find($sale->customer_id)->customer_no}} )
#else
Guest
#endif
</td>
<td class="text-center">
#php
$total = $sale->sub_total + $sale->vat_amount;
echo number_format($total);
#endphp
</td>
<td class="text-center">
#php
$total_discount = number_format($total_discount + ($sale->discount_amount + $sale->coupon_discount_amount));
echo number_format($sale->discount_amount + $sale->coupon_discount_amount);
#endphp
</td>
<td class="text-center">
#php
echo number_format($sale->net_amount);
#endphp
</td>
<td class="text-center">
#if(isset($sale->id))
#php
$advance = App\SalePaid::where('sale_id', $sale->id)->sum('paid_amount');
if($advance <= $sale->net_amount){
echo number_format($advance);
}else{
echo number_format($sale->net_amount);
$advance = $sale->net_amount;
}
$total_advance = $total_advance + $advance;
#endphp
#endif
</td>
<td class="text-center">
#php
echo number_format(max(($sale->net_amount - App\SalePaid::where('sale_id', $sale->id)->sum('paid_amount')), 0));
$total_net_sum = $total_net_sum + $sale->net_amount;
#endphp
</td>
<td class="text-center">
#if(isset($sale->company_currency_id))
{{
App\Currency::find(App\CompanyCurrency::find($sale->company_currency_id)->currency_id)->code
}}
#else
N/A
#endif
</td>
</tr>
#endforeach
#endif
</tbody>
</table>
</div>
<br>
<div class="panel-heading title-color">
<table class="table table-hover table-heading" id="tableWithSearch">
<tbody class="table-body">
<tr>
<td class="v-align-middle">
<h5 class="font-montserrat all-caps small hint-text semi-bold">Discount: BDT
#php
echo number_format($total_discount);
#endphp</h5>
</td>
<td class="v-align-middle">
<h5 class="font-montserrat all-caps small hint-text semi-bold">Advance: BDT
#php
echo number_format($total_advance);
#endphp
</h5>
</td>
<td class="v-align-middle">
<h5 class="font-montserrat all-caps small hint-text semi-bold">Remain Sum: BDT
#php
echo number_format(max(($total_net_sum - $total_advance), 0));
#endphp
</h5>
</td>
<td class="v-align-middle">
<h5 class="font-montserrat all-caps small hint-text bold">Net. Sum: BDT
#php
echo number_format($total_net_sum);
#endphp
</h5>
</td>
</tr>
</tbody>
</table>
</div><br/>
<div class="stamp-signature">
<div class="authority-signature">
Printed By: {{ Auth::user()->userDetail ? Auth::user()->userDetail->first_name : '' }} {{ Auth::user()->userDetail ? Auth::user()->userDetail->last_name : '.......................' }}
</div>
<div class="customer-signature">
Authorized By: .......................
</div>
</div>
<div class="clear"></div>
<br>
<br>
<div>
<span class="bold hint-text">Notes: </span>
<span class="small hint-text">This report generated on Date: {{ $report_generated_on }}. Please issue company stamp and sign in above section.
</span>
</div>
<div class="footer-border" style="width: 100% !important;"></div>
<footer>
<div class="footer-logo">
<img style="width: 80px; height: 22px;" alt="" class="invoice-logo" src="{{ asset('uploads/company/'.$company_details->id.'/receipt_logo/50x50/'.$company_details->receipt_logo)}}">
</div>
#if($out_let)
<div class="footer-info-text">
| <span class="m-l-70 text-black sm-pull-right"> {{ $out_let[0]->name }} </span>
| <span class="m-l-70 text-black sm-pull-right">
{{ $out_let[0]->phone }}</span> <br/><br/>
<span class="m-l-70 text-black sm-pull-right"> Software By: {{ env('APP_URL')}}</span><br/>
</div>
#endif
</footer>
</div>
</div>
</div>
<!-- END card -->
</div>
<!-- END CONTAINER FLUID -->
#endsection
Here comes controller function:
/**
* Generate daily sales report for logged user's Outlet
*
* #return Pdf generated report
*/
protected function dailyReport(){
$company_details = $this->getCompanyDetails();
$report_for = "Daily Sales Report";
$report_for_date = "Date: ".date('Y-m-d H:i:s');
$report_for_month = "";
$from_date = $today_date = date('Y-m-d');
$report_generated_on = date('Y-m-d H:i:s');
$out_let = OutLet::where('id',$_COOKIE['out_let_id'])->get();
$sales = Sale::whereDate('sale_date',date('Y-m-d'))->where('out_let_id',$_COOKIE['out_let_id'])->get();
$pdf = PDF::loadView('dashboard.sales-report', compact('out_let','sales', 'report_for','report_for_date','from_date','today_date','report_for_month','report_generated_on','company_details'));
// (Optional) Setup the paper size and orientation
$pdf->setPaper('A4', 'portrait');
// download PDF file with download method
$sales_report_file_name = "daily_sales_".date('Y-m-d').".pdf";
return $pdf->download($sales_report_file_name);
}
Here comes new blade file code where no DB query:
#extends('layouts.invoice_master')
#section('content')
#php
$total_discount = 0;
$total_advance = 0;
$total_net_sum = 0;
$total = 0;
#endphp
<!-- START CONTAINER FLUID -->
<div class=" container-fluid container-fixed-lg">
<!-- START card -->
<div class="card card-default m-t-20">
<div class="card-body">
<!-- Define header and footer blocks before your content -->
<header>
<p>{{ $report_for }} </p>
<p>{{ $report_for_date }}</p>
<p>{{ $report_for_month }}</p>
</header>
<div class="invoice padding-50 sm-padding-10" style="padding-left:unset !important;">
<div class="row">
<div class="col-lg-3" style="padding-left:5px !important;">
<img style="width: 200px;margin-bottom: 20px" alt="" class="invoice-logo" src="{{ asset('uploads/company/'.$company_details->id.'/receipt_logo/'.$company_details->receipt_logo)}}">
</div>
<div class="col-lg-8">
<address class="m-t-10">
#if($out_let)
{{ $out_let[0]->name }}
<br>{{ $out_let[0]->location }}
<br>{{ $out_let[0]->address }}
<br>{{ $out_let[0]->city }} , {{ $out_let[0]->zip }}
<br>Contact: {{ $out_let[0]->phone }}
<br>
#endif
</address>
</div>
</div>
<div class="clearfix"></div>
<div class="table-responsive table-invoice">
<table class="table m-t-25">
<thead>
<tr>
<th class="text-center">Date</th>
<th class="text-center">Invoice</th>
<th class="text-center">Customer</th>
<th class="text-center">Total</th>
<th class="text-center">Discount</th>
<th class="text-center">Net.Sum</th>
<th class="text-center">Advance</th>
<th class="text-center">Remain</th>
<th class="text-center">Currency</th>
</tr>
</thead>
<tbody>
#if( $sales )
#foreach( $sales as $sale )
<tr>
<td class="text-center">
{{ $sale->created_at }}
</td>
<td class="text-center">
<p class="text-black">
#if(isset($sale->invoice_prefix_id))
Saj-{{ $sale->invoice_no
}}
#else
{{ $sale->invoice_no }}
#endif
</p>
</td>
<td class="text-center">
#if(isset($sale->customer_id))
abu taher
#else
Guest
#endif
</td>
<td class="text-center">
#php
$total = $sale->sub_total + $sale->vat_amount;
echo number_format($total);
#endphp
</td>
<td class="text-center">
#php
$total_discount = number_format($total_discount + ($sale->discount_amount + $sale->coupon_discount_amount));
echo number_format($sale->discount_amount + $sale->coupon_discount_amount);
#endphp
</td>
<td class="text-center">
#php
echo number_format($sale->net_amount);
#endphp
</td>
<td class="text-center">
#if(isset($sale->id))
#php
$advance = "250";
if($advance <= $sale->net_amount){
echo number_format($advance);
}else{
echo number_format($sale->net_amount);
$advance = $sale->net_amount;
}
$total_advance = $total_advance + $advance;
#endphp
#endif
</td>
<td class="text-center">
#php
echo number_format(max(($sale->net_amount - 250), 0));
$total_net_sum = $total_net_sum + $sale->net_amount;
#endphp
</td>
<td class="text-center">
#if(isset($sale->company_currency_id))
BDT
#else
N/A
#endif
</td>
</tr>
#endforeach
#endif
</tbody>
</table>
</div>
<br>
<div class="panel-heading title-color">
<table class="table table-hover table-heading" id="tableWithSearch">
<tbody class="table-body">
<tr>
<td class="v-align-middle">
<h5 class="font-montserrat all-caps small hint-text semi-bold">Discount: BDT
#php
echo number_format($total_discount);
#endphp</h5>
</td>
<td class="v-align-middle">
<h5 class="font-montserrat all-caps small hint-text semi-bold">Advance: BDT
#php
echo number_format($total_advance);
#endphp
</h5>
</td>
<td class="v-align-middle">
<h5 class="font-montserrat all-caps small hint-text semi-bold">Remain Sum: BDT
#php
echo number_format(max(($total_net_sum - $total_advance), 0));
#endphp
</h5>
</td>
<td class="v-align-middle">
<h5 class="font-montserrat all-caps small hint-text bold">Net. Sum: BDT
#php
echo number_format($total_net_sum);
#endphp
</h5>
</td>
</tr>
</tbody>
</table>
</div><br/>
<div class="stamp-signature">
<div class="authority-signature">
Printed By: ABU TAHER
.......................
</div>
<div class="customer-signature">
Authorized By: .......................
</div>
</div>
<div class="clear"></div>
<br>
<br>
<div>
<span class="bold hint-text">Notes: </span>
<span class="small hint-text">This report generated on Date: {{ $report_generated_on }}. Please issue company stamp and sign in above section.
</span>
</div>
<div class="footer-border" style="width: 100% !important;"></div>
<footer>
<div class="footer-logo">
<img style="width: 80px; height: 22px;" alt="" class="invoice-logo" src="{{ asset('uploads/company/'.$company_details->id.'/receipt_logo/50x50/'.$company_details->receipt_logo)}}">
</div>
#if($out_let)
<div class="footer-info-text">
| <span class="m-l-70 text-black sm-pull-right"> {{ $out_let[0]->name }} </span>
| <span class="m-l-70 text-black sm-pull-right">
{{ $out_let[0]->phone }}</span> <br/><br/>
<span class="m-l-70 text-black sm-pull-right"> Software By: {{ env('APP_URL')}}</span><br/>
</div>
#endif
</footer>
</div>
</div>
</div>
<!-- END card -->
</div>
<!-- END CONTAINER FLUID -->
#endsection

Finally I solved the issue myself. The problem was in img tag image loading path. Previously using below img tag where path was wrong according to pdf generation and due to that system was unable to load the image, so it was taking too much time:
Previous img tag:
<img style="width: 80px; height: 22px;" alt="" class="invoice-logo" src="{{ asset('uploads/company/'.$company_details->id.'/receipt_logo/50x50/'.$company_details->receipt_logo)}}">
New img tag where image path is different than upper one, and here everything working fine including time.
<img style="width: 80px; height: 22px;" alt="" class="invoice-logo" src="{{ public_path().'/uploads/company/'.$company_details->id.'/receipt_logo/50x50/'.$company_details->receipt_logo }}">

Related

Blade filter data depending on the authenticated user's role

I'm trying to make a school management system with laravel and jQuery. Currently it's working ok with admin as logged in user. But I'm having trouble to create the blade for when the parents are logged in.
I've in a StudentController the index formula below
if( Auth::user()->is_parent ){
$students = Student::with(['status', 'birth_country', 'main_nationality', 'secondary_nationality', 'spoken_languages', 'student_categories', 'p_1_relationship',
'p_1_main_nationality', 'p_1_pref_comm_lang', 'p_2_relationship', 'p_2_main_nationality', 'p_2_pref_comm_lang', 'academic_session',
'academic_formation', 'academic_class', 'registration_type', 'academic_section', 'option_1', 'option_2', 'option_3', 'option_4', 'option_5',
'option_6', 'option_7', 'option_8', 'option_9', 'option_10', 'user_student', 'created_by'])
->where('p_1_email', '=', Auth::user()->email)
->orWhere('p_2_email', '=', Auth::user()->email)
->get();
} else{
$students = Student::with(['status', 'birth_country', 'main_nationality', 'secondary_nationality', 'spoken_languages', 'student_categories', 'p_1_relationship',
'p_1_main_nationality', 'p_1_pref_comm_lang', 'p_2_relationship', 'p_2_main_nationality', 'p_2_pref_comm_lang', 'academic_session',
'academic_formation', 'academic_class', 'registration_type', 'academic_section', 'option_1', 'option_2', 'option_3', 'option_4', 'option_5',
'option_6', 'option_7', 'option_8', 'option_9', 'option_10', 'user_student', 'created_by'])
->get();
}
$statuses = Status::where('module', 'Academic Registration')->get();
$countries = Country::get();
$languages = Language::get();
$categories = Category::where('head_category_id',24)->get();
$relationships = Relationship::get();
$academic_sessions = AcademicSession::get();
$academic_formations = AcademicFormation::get();
$academic_classes = AcademicClass::get();
$types = Type::where('module', 'Academic Registration')->get();
$academic_sections = AcademicSection::get();
$academic_subjects = AcademicSubject::get();
$users = User::get();
return view('app.students.index', compact('academic_classes', 'academic_formations', 'academic_sections', 'academic_sessions', 'academic_subjects', 'categories',
'countries', 'languages', 'relationships', 'statuses', 'types', 'students', 'users'));
And my index blade ,
#if ( Auth::user()->is_parent )
<table class=" table table-bordered table-striped table-hover table-sm">
<thead>
<tr>
<th width="6" class="text-center">
</th>
<th width="10" class="text-center">
{{ trans('student.fields.status') }}
</th>
<th class="text-center">
{{ trans('student.fields.student_category') }}
</th>
<th class="text-center">
{{ trans('student.fields.first_registration') }}
</th>
<th class="text-center">
{{ trans('student.fields.first_name') }}
</th>
<th class="text-center">
{{ trans('student.fields.last_name') }}
</th>
<th class="text-center">
{{ trans('student.fields.birth_date') }}
</th>
<th class="text-center">
{{ trans('cruds.student.fields.academic_session') }}
</th>
<th class="text-center">
{{ trans('student.fields.academic_formation') }}
</th>
<th class="text-center">
{{ trans('student.fields.academic_class') }}
</th>
<th class="text-center">
{{ trans('student.fields.registration_type') }}
</th>
<th class="text-center">
</th>
</tr>
</thead>
<tbody>
#foreach($students as $student)
<tr>
<td class="text-center">
</td>
<td class="text-center">
{{ $student->status->name ?? '' }}
</td>
<td class="text-center">
#foreach($student->student_categories as $key => $item)
<span class="label label-info label-many">{{ $item->name }}</span>
#endforeach
</td>
<td class="text-center">
<span style="display:none">{{ $student->first_registration ?? '' }}</span>
<input type="checkbox" disabled="disabled" {{ $student->first_registration ? 'checked' : '' }}>
</td>
<td class="text-center">
{{ $student->first_name ?? '' }}
</td>
<td class="text-center">
{{ $student->last_name ?? '' }}
</td>
<td class="text-center">
{{ $student->birth_date ?? '' }}
#if ( $student->birth_date )
<ul class="list-unstyled">
<li>
{{ trans('student.fields.age') }} : {{ $student->age ?? '' }} years
</li>
</ul>
#endif
</td>
<td class="text-center">
{{ $student->academic_session->name ?? '' }}
</td>
<td class="text-center">
{{ $student->academic_formation->code ?? '' }}
</td>
<td class="text-center">
{{ $student->academic_class->name ?? '' }}
</td>
<td class="text-center">
{{ $student->registration_type->name ?? '' }}
<ul>
#if ( $student->entry_date )
<li>
{{ trans('student.fields.entry_date') }} : {{ $student->entry_date ?? '' }}
</li>
#endif
#if ( $student->exit_date )
<li>
{{ trans('student.fields.entry_date') }} : {{ $student->exit_date ?? '' }}
</li>
#endif
</ul>
</td>
<td class="text-center">
<ul class="list-unstyled">
<li>
#can('student_show')
<a class="btn btn-xs btn-primary" href="{{ route('app.students.show', $student->id) }}">
{{ trans('view') }}
</a>
#endcan
</li>
<li>
#can('student_edit')
<a class="btn btn-xs btn-info" href="{{ route('app.students.edit', $student->id) }}">
{{ trans('edit') }}
</a>
#endcan
</li>
<li>
#can('student_delete')
<form action="{{ route('app.students.destroy', $student->id) }}" method="POST" onsubmit="return confirm('{{ trans('are You Sure') }}');" style="display: inline-block;">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="btn btn-xs btn-danger" value="{{ trans('delete') }}">
</form>
#endcan
</li>
</ul>
</td>
</tr>
#endforeach
</tbody>
</table>
#else
.........
#endif
When I logged in as admin, the index datatable is working fine, but when I logged in as a parent which as children listed, the children aren't showed.
What did I miss?

cart total only calculating for the first 2 products only in laravel

I have 2 types of products in my app.Products with attribute and products without attribute. I can successfully add the products to cart, but I am having an issue whereby on the part of showing the grand total,only the total of the first 2 products subtotal are calculated and shown.I haven't understood where the bug is. I have tried debugging the code but still can't figure it out. Here is the HTML code that process the grand total price.
//table that shows the cart details and where the subtotal is calculated
<table class="userdatatable table table-striped table-bordered nowrap" style="width:100%; border:2px solid black;">
<thead>
<tr>
<th>Product Name</th>
<th>Price</th>
<th>Image</th>
<th>Quantity</th>
<th>Discount</th>
<th>Total</th>
<th>Remove</th>
</tr>
</thead>
<body>
<?php $total_price=0; ?>
<?php $attributetotal_price=0; ?>
<?php $noattributetotal_price=0; ?>
#foreach($cartitems as $item)
<tr>
#if ($item->product->is_attribute==1)
<?php $attrpric=Merchadise::getdiscountedattrprice($item['product_id'],$item['size']);
?>,
#else
<?php $discountedprice=Merchadise::getdiscountedprice($item['product_id']);
?>
#endif
<td>{{ $item->product->merch_name }}</td>
#if ($item->product->is_attribute==1)
<td>{{ $attrpric['merch_price'] }}</td>
#else
<td>{{ $item->product->merch_price }}</td>
#endif
<td>
<img src="{{ asset ('images/productimages/small/'.$item->product->merch_image) }}" style="width:100px; height:100px;" alt="Product">
</td>
<td>
<button class="itemupdate qtyminus" type="button" data-cartid="{{ $item->id }}">
<i class="fa fa-minus" aria-hidden="true"></i>
</button>
<input data-id={{ $item->id }} class="quantity" min="1" name="quantity[]" value="{{ $item->quantity }}" type="number">
<button class="itemupdate qtyplus" type="button" data-cartid="{{ $item->id }}">
<i class="fa fa-plus" aria-hidden="true"></i>
</button>
</td>
#if ($item->product->is_attribute==1)
<td>sh.{{ $attrpric['discount'] * $item['quantity'] }}</td>
<td>sh.{{ $attrpric['final_price'] * $item['quantity'] }}</td>
#elseif($item->product->is_attribute==0)
<td>{{ ($item->product->merch_price-$discountedprice) * $item['quantity'] }}</td>
<td>{{ $discountedprice * $item['quantity'] }}</td>
#endif
<td>
<a class="btn btn-primary btn-xs" onclick="confirm return('Are you Sure You want to Delete?')" href="{{ route('deletecartitem', $item->id) }}"><i class="fa fa-trash"></i></a>
</td>
</tr>
{{-- show cart total --}}
#if ($item->product->is_attribute==1)
<?php $attributetotal_price=$total_price+($attrpric['final_price'] * $item['quantity'] );
?>
#elseif($item->product->is_attribute==0)
<?php $noattributetotal_price=$total_price+($discountedprice * $item['quantity'] );?>
#endif
#endforeach
</tbody>
</table>
<div class="row">
<div class="col-md-5 ml-auto">
<div class="cart-page-total">
<h2>Cart totals</h2>
<ul class="mb-20">
<li>Coupon Discount
<span class="couponAmount">
#if (Session::has('couponAmount'))
-Sh.{{ Session::get('couponAmount') }}
#else
sh.0
#endif
</span>
</li>
<li>Grand Total
<span class="grand_total">Sh.{{ $attributetotal_price+$noattributetotal_price-Session::get('couponAmount') }}</span>
</li>
</ul>
#auth
Checkout <i class="fa fa-angle-right"></i>
#else
<p>To proceed to checkout create or log in to your account...</p>
<span href="#" data-toggle="modal" data-target="#RegistrationModal" class="btn btn-success btn-block">Create/Login an Account<i class="fa fa-angle-right"></i></span>
#endauth
</div>
</div>
</div>
here is my products migration table
it was just a minor bug.i removed the $total_price
{{-- show cart total --}}
#if ($item->product->is_attribute==1)
<?php $attributetotal_price=$attributetotal_price+($attrpric['final_price'] * $item['quantity'] );
?>
#elseif($item->product->is_attribute==0)
<?php $noattributetotal_price=$noattributetotal_price+($discountedprice * $item['quantity'] );?>
#endif

Trix editor doesn't show up when used inside a #if condition

In larave8 livewire I want to use the trix editor to create an bladeview on the livewire component InfoletterCrud is the following
<div class="container ml-auto mr-auto p-10 min-h-screen">
<div class="page-title ">{{ $page_title }}</div>
<div class="button-line flex flex-col">
#if ($isEditMode)
#if ($showMode)
#include('livewire.show-infoletter')
#else
#include('livewire.create-infoletter')
#endif
#else
<div>
<button wire:click="create()" class="button-cancel text-white font-bold py-2 px-4 rounded my-3">Ajouter une info-lettre</button>
</div>
#if (count($infoletters) > 0)
<table class="w-full table-fixed admin-stripped-table">
<thead>
<tr class="w-full">
<th class="w-1/12">Id</th>
<th class="w-10/12">Titre</th>
<th class="w-1/12">Actions</th>
</tr>
</thead>
<tbody>
#foreach ($infoletters as $infoletter)
<tr>
<td>{{ $infoletter->id }}</td>
<td>{{ $infoletter->title }}</td>
<td>
<div style="display:flex">
<button
wire:click="show({{ $infoletter->id }})"
class="fontawesome-icon">
<span data-toggle="tooltip" title="Voir" class="fa fa-paper-plane fa-xs "></span>
</button>
<button
wire:click="edit({{ $infoletter->id }})"
class="fontawesome-icon">
<span data-toggle="tooltip" title="Modifier" class="fa fa-edit fa-xs "></span>
</button>
<button
wire:click="$emit('openModal', 'livewire-modal', {{ json_encode(['type' => 'L\'infolettre', 'ident' => $infoletter->id]) }})"
class="fontawesome-icon">
<span data-toggle="tooltip" title="Supprimer" class=" fa fa-trash fa-xs "></span>
</button>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>,
#else
<p>Il n'y a aucune info-lettre</p>
#endif
#endif
</div>
</div>
When I place the trix component with #livewire('trix') just above the #if (isEditMode) test, it shows up. But it is not what I want because I am not in the Edit mode.
If I place it in the livewire.create-infoletter include, it doesn't show up.
Even when I place it just after the #if (isEditMode) test, it doesn't show up.
What is the trouble
You don't show your component and how you're initializing Trix, so this is just a guess. I'm assuming you're doing it with Alpinejs, and that Alpine has already made its DOM updates by the time the #if condition executes. Try calling your initialization function using $nextTick - see here: https://alpinejs.dev/magics/nextTick.

Laravel foreach in if statement

This is my code
#if ($story)
<table class="table-auto my-3 w-full">
<thead class="justify-between">
<tr class="bg-gray-800">
<th class="px-16 py-2">
<span class="text-gray-300">#Id</span>
</th>
<th class="px-16 py-2">
<span class="text-gray-300">Name</span>
</th>
<th class="px-16 py-2">
<span class="text-gray-300">Description</span>
</th>
<th class="px-16 py-2">
<span class="text-gray-300">Actions</span>
</th>
</tr>
</thead>
<tbody class="bg-gray-200">
#foreach ($story as $item)
<tr class="bg-white border-4 border-gray-200">
<td class="px-16 py-2 flex flex-row items-center">
{{ $item->id }}
</td>
<td>
{{ Str::words($item->name, 3, $end = '...') }}
</td>
<td class="px-16 py-2">
{!! Str::words($item->description, 6, $end = '...') !!}
</td>
<td class="px-16 py-2">
<a href="{{ route('dashboard.story.edit', $item->id) }}">
<button class="bg-blue-500 text-white px-4 py-1 border rounded-md hover:bg-white hover:border-blue-500 hover:text-black">
Edit
</button>
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
#else
<p>No results found</p>
#endif
This is my controller
public function index()
{
$story = Story::paginate(10);
return view('admin.story.index', ['story' => $story]);
}
I'm using Laravel 8 and I don't want to show table headers when the value is null. Laravel showing tables headers and not showing the 'No results found' message.
Any help would be appreciated.
when you use paginate, your data split. so if you want check this data empty or not just use
#if( $story->total() )
just replace it with
#if ( $story )

Laravel - How to resolve Error 500: Internal server error when $currentstatus = NULL

In my Laravel-5.8 project I have this code:
public function manager_employee_goal($id)
{
$userCompany = Auth::user()->company_id;
$userEmployee = Auth::user()->employee_id;
$identities = DB::table('appraisal_identity')->select('id')->where('company_id', $userCompany)->where('is_current', 1)->first();
$linemanager = DB::table('hr_employees')->select('line_manager_id')->where('id', $userEmployee)->first();
$linemanageremployee = DB::table('hr_employees')->select('id')->where('line_manager_id', $linemanager->line_manager_id)->pluck('id');
$appraisedemployees = HrEmployee::select('id', 'employee_code', 'first_name', 'last_name')->where('id', $id)->get();
$goals = AppraisalGoal::where('employee_id', $id)->where('appraisal_identity_id', $identities->id)->where('is_published', 1)->where('id', '!=', $linemanager->line_manager_id)->get();
$currentstatus = AppraisalGoal::select('is_approved')->where('employee_id', $id)->where('appraisal_identity_id', $identities->id)->where('is_published', 1)->where('id', '!=', $linemanager->line_manager_id)->first();
$incompleteCount = $goals->filter(function($item) {
return $item->is_approved == 1;
})->count();
return view('appraisal.appraisal_goals.manager_employee_goal')->with('goals', $goals)->with('appraisedemployees', $appraisedemployees)->with('incompleteCount', $incompleteCount)->with('currentstatus', $currentstatus);
}
view
<div class="col-md-12">
<!-- general form elements -->
<div class="card card-secondary">
<div class="card-header">
<h3 class="card-title">Goal Review</h3>
<p class="d-flex flex-column text-right">
<span class="font-weight-bold">
Current Status:
#if ($currentstatus->is_approved == 3)
<span class="badge bg-success" >Approved</span>
#elseif ($currentstatus->is_approved == 2)
<span class="badge bg-danger">Not Approved</span>
#elseif ($currentstatus->is_approved == 1)
<span class="badge bg-info">Awaiting Approval</span>
#else
<span class="badge bg-black">Draft</span>
#endif
</span>
</p>
</div>
<!-- /.card-header -->
<!-- form start -->
<div class="card-body">
<div class="table-responsive">
<table class=" table table-bordered table-striped table-hover datatable">
<thead>
<tr>
<th width="8%">
Goal Type
</th>
<th width="20%">
Goal Title & Description
</th>
<th width="20%">
Activities
</th>
<th width="20%">
KPIs
</th>
<th width="11%">
Start Date - End Date
</th>
<th>
Weight(%)
</th>
<th>
</th>
</tr>
</thead>
<tbody>
#foreach($goals as $key => $goal)
<td>
<!--{{ $goal->goaltype ? $goal->goaltype->name : 'Uncategorized' }}-->
{{$goal->goaltype->name ?? '' }}
</td>
<td>
<b>{{$goal->goal_title ?? '' }}</b>
<br><br>
{!! Str::words($goal->goal_description, 20, ' ...') !!}
</td>
<td>
#foreach($goal->appraisalgoaldetail as $key => $appraisalgoaldetail)
<ul class="list-unstyled">
<li>
<!--{{$key+1}}. {{$appraisalgoaldetail->activity}}-->
{{$key+1}}. {!! Str::words($appraisalgoaldetail->activity, 10, ' ...') !!}
</li>
</ul>
#endforeach
</td>
<td>
#foreach($goal->appraisalgoaldetail as $key => $appraisalgoaldetail)
<ul class="list-unstyled">
<li>
<!--{{$key+1}}. {{$appraisalgoaldetail->kpi_description}}-->
{{$key+1}}. {!! Str::words($appraisalgoaldetail->kpi_description, 10, ' ...') !!}
</li>
</ul>
#endforeach
</td>
<td>
{{Carbon\Carbon::parse($goal->appraisal_start_date)->format('M d, Y') ?? '' }} - {{Carbon\Carbon::parse($goal->appraisal_end_date)->format('M d, Y') ?? '' }}
</td>
<td>
{{$goal->weighted_score ?? '' }}
</td>
<td>
#if(in_array($goal->is_approved, [1, 2, 3]))
<a class="btn btn-xs btn-primary" href="{{ route('appraisal.appraisal_goals.manager_employee_goal_list', ['id'=>$goal->id]) }}">
{{ trans('global.view') }}
</a>
#endif
#if(in_array($goal->is_approved, [1]))
<a class="btn btn-xs btn-info" data-toggle="modal" data-target="#comment{{ $goal->id }}" data-original-title="Comment">Comment</a>
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<div class="row no-print">
<div class="col-12">
#if ($incompleteCount)
<i class="fas fa-check"></i> Approve
<i class="fas fa-minus-circle"></i> Reject
#endif
</div>
</div>
</div>
</div>
</div>
When I wanted to load the page I got this error:
Error 500: Internal server error
However when I
die(var_dump($incompleteCount));
I got
int(0)
and
die(var_dump($currentstatus));
I got
NULL
At last when I removed:
Current Status:
#if ($currentstatus->is_approved == 3)
<span class="badge bg-success" >Approved</span>
#elseif ($currentstatus->is_approved == 2)
<span class="badge bg-danger">Not Approved</span>
#elseif ($currentstatus->is_approved == 1)
<span class="badge bg-info">Awaiting Approval</span>
#else
<span class="badge bg-black">Draft</span>
#endif
from the view blade, the error vanished.
How do I resolve this?
Thank you
#AndySong has pointed out the issue, you are trying to get a property on null.
A couple of solutions:
If you cannot find the current status, should the page still load? If not, use firstOrFail() :
Example -
$currentstatus = AppraisalGoal::select('is_approved')
->where('employee_id', $id)
->where('appraisal_identity_id', $identities->id)
->where('is_published', 1)
->where('id', '!=', $linemanager->line_manager_id)
->firstOrFail();
Change your if statements around for current status to check if current status is null:
Example -
Current Status:
#if (is_null($currentstatus))
<span class="badge bg-black">Draft</span>
#elseif ($currentstatus->is_approved == 3)
<span class="badge bg-success" >Approved</span>
#elseif ($currentstatus->is_approved == 2)
<span class="badge bg-danger">Not Approved</span>
#elseif ($currentstatus->is_approved == 1)
<span class="badge bg-info">Awaiting Approval</span>
#else
<span class="badge bg-black">Draft</span>
#endif

Resources