I have this code in my controller:
public function data_karyawan()
{
return view('data-karyawan', [
"title" => "Data Karyawan",
"karyawan" => Karyawan::with(['role', 'user'])->search(request(['search']))->paginate(10)
]);
}
but when I tried to give pagination on my view like this, there is an error:
{{ $karyawan->links() }}
what did I do wrong?
Blade file:
#extends('layouts.main')
#section('container')
<div class="container-fluid">
<div class="row">
<nav id="sidebarMenu" class="col-md-2 col-lg-2 d-md-block bg-light sidebar collapse">
<div class="position-sticky pt-3">
<div class="d-flex flex-column flex-shrink-0 p-3 bg-light" style="width: 200px;">
<svg class="bi me-2" width="40" height="0"></svg>
<span class="fs-10"><center><img src="/images/logo/selindo4.png" style="width:100px"></center></span>
</a>
<hr>
<ul class="nav nav-pills flex-column mb-auto">
<li class="nav-item">
<a href="/admin" class="nav-link link-dark">
<svg class="bi me-2" width="16" height="40"><use xlink:href="#home"/></svg>
Home
</a>
</li>
<li>
<a href="#" class="nav-link active" aria-current="page">
<svg class="bi me-2" width="16" height="40"><use xlink:href="#people-circle"/></svg>
Data Karyawan
</a>
</li>
<li>
<a href="/admin/data-departemen" class="nav-link link-dark">
<svg class="bi me-2" width="16" height="40"><use xlink:href="#speedometer2"/></svg>
Departemen
</a>
</li>
<li>
<a href="/admin/data-cuti" class="nav-link link-dark">
<svg class="bi me-2" width="16" height="40"><use xlink:href="#table"/></svg>
Summary Cuti
</a>
</li>
</ul>
<hr>
<div class="dropdown">
<a href="#" class="d-flex align-items-center link-dark text-decoration-none dropdown-toggle" id="dropdownUser2" data-bs-toggle="dropdown" aria-expanded="false">
<img src="/images/avatar/avatar-2.png" alt="" width="32" height="40" class="rounded-circle me-2">
<strong>Admin</strong>
</a>
<ul class="dropdown-menu text-small shadow" aria-labelledby="dropdownUser2">
<li><a class="dropdown-item" href="#">Logout</a></li>
</ul>
</div>
</div>
</div>
</nav>
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2" style="position:absolute margin: auto auto"><p></p>Data Karyawan</h1>
</div>
<div class="row">
<div class="col-md-6">
<form action="/admin/data-karyawan">
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Cari Karyawan" name = "search" value="{{ request('search') }}">
<button class="btn btn-outline-primary" type="submit">Search</button>
</div>
</form>
</div>
</div>
<center>
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<hr>
<thead>
<tr>
<th>No.</th>
<th>NIK</th>
<th>Nama</th>
<th>JK</th>
<th>Tempat Lahir</th>
<th>Tanggal Lahir</th>
<th>Alamat</th>
<th>Agama</th>
<th>Jabatan</th>
<th>Divisi</th>
<th>Role</th>
<th>E-mail</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
foreach ($karyawan as $karyawan) { ?>
<tr>
<td>{{ $i++ }}</td>
<td>{{ $karyawan["nik"] }}</td>
<td>{{ $karyawan["nama"] }}</td>
<td>{{ $karyawan["jk"] }}</td>
<td>{{ $karyawan["tempat_lahir"] }}</td>
<td>{{ $karyawan["tanggal_lahir"] }}</td>
<td>{{ $karyawan["alamat"] }}</td>
<td>{{ $karyawan["agama"] }}</td>
<td>{{ $karyawan["jabatan"] }}</td>
<td>{{ $karyawan["departemen"] }}</td>
<td>{{ $karyawan->role->nama_role }}</td>
<td>{{ $karyawan->user->email }}</td>
<td width = '180px'>
Update
Delete
</td>
</tr>
<?php } ?>
</tbody>
</table>
</center>
<table>
<tr> <td>Tambah Data Karyawan</td>
<td width='100px'> </td><td width='100px'> </td><td width='100px'> </td>
<td width='100px'> </td><td width='100px'> </td><td width='100px'> </td>
<td width='100px'> </td><td width='100px'> </td><td width='25px'> </td>
</table>
</div>
</main>
</div>
</div>
{{ $karyawan->links() }}
#endsection
thank you very much.
There is an issue here :
$karyawan as $karyawan
you override the value
In your blade file, you use the same variables for your foreach statement. Thefore overriding one.
Change foreach ($karyawan as $karyawan) to something like foreach ($karyawan as $kar) and then replace all occurances of $karyawan in the below block with $kar
<td>{{ $karyawan["nik"] }}</td>
<td>{{ $karyawan["nama"] }}</td>
<td>{{ $karyawan["jk"] }}</td>
<td>{{ $karyawan["tempat_lahir"] }}</td>
<td>{{ $karyawan["tanggal_lahir"] }}</td>
<td>{{ $karyawan["alamat"] }}</td>
<td>{{ $karyawan["agama"] }}</td>
<td>{{ $karyawan["jabatan"] }}</td>
<td>{{ $karyawan["departemen"] }}</td>
<td>{{ $karyawan->role->nama_role }}</td>
<td>{{ $karyawan->user->email }}</td>
<td width = '180px'>
Update
Delete
</td>
Related
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 }}">
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
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 )
I would like to know how to put a list of the last 5 items in the dashboard using backpack?
I made the table
<div class="table-responsive">
<table class="table table-hover m-0 table-actions-bar">
<thead>
<tr>
<th>
<div class="btn-group dropdown">
<button type="button" class="btn btn-light btn-xs dropdown-toggle waves-effect waves-light"
data-toggle="dropdown" aria-expanded="false"><i class="mdi mdi-chevron-down"></i></button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Dropdown link</a>
<a class="dropdown-item" href="#">Dropdown link</a>
</div>
</div>
</th>
<th>Titolo Post</th>
<th>Data</th>
<th>Stato</th>
<th>Categria</th>
<th>Azione</th>
</tr>
</thead>
<tbody>
<tr>
<td>
</td>
<td>
<h5 class="m-0 font-weight-medium"></h5>
</td>
<td>
<i class="mdi mdi-map-marker text-primary"></i>
</td>
<td>
<i class="mdi mdi-clock-outline text-success"></i>
</td>
<td>
<i class="mdi mdi-currency-usd text-warning"></i>
</td>
<td>
<i class="mdi mdi-pencil"></i>
<i class="mdi mdi-close"></i>
</td>
</tr>
</tbody>
</table>
</div>
I made the table inside the jumbotron.blade.php then the function that prints you on screen the last 5 posts I put it here? within the dashboard method?
$recentPost : Article::orderBy('id', 'desc')>limit(5)->get()
any other solution?
This line will get you the last 5 articles.
$articles = Article::orderBy('id', 'desc')->limit(5)->get();
You need to pass it to the view. Ex:
return view('dashboard', ['articles' => $articles]);
And loop the articles on the blade file table. Ex:
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Title</th>
<th scope="col">Author</th>
<th scope="col">Date</th>
</tr>
</thead>
<tbody>
#foreach($articles as $article)
<tr>
<th scope="row">{{ $article->id }}</th>
<td>{{ $article->title }}</td>
<td>{{ $article->author }}</td>
<td>{{ $article->created_at }}</td>
</tr>
#endforeach
</tbody>
</table>
I'm currently trying to do multiple deletion in Laravel 5.3. I'd like to be something just like how emails are deleted using checkbox.
But before deleting, I'd like to do confirmation using a modal.
form in my blade looks like this
<form id="linkForm" action="/links/deleteLinks" method="POST">
{{ csrf_field() }}
{{ method_field('POST') }}
<div class="ibox-content">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover dataTables-example footable toggle-arrow-tiny">
<div class="btn-group btn-group-sm pull-right">
<a data-toggle="modal" class="btn btn-primary" href="#modal-form-add" data-dismiss="modal"><i class="fa fa-plus-square-o"></i> Add</a>
<input class="btn btn-danger" type="submit" value="Delete">
</div>
<thead>
<tr>
<th></th>
<th data-toggle="true" data-hide="all">Company</th>
{{-- <th>Category</th> --}}
<th>Page Url</th>
<th data-hide="all">Domain Url</th>
<th>Destination Url</th>
<th>Contact Email</th>
<th data-hide="all">Trust Flow</th>
<th data-hide="all">Estimated Traffic</th>
<th data-hide="all">Domain Authority</th>
<th>Status</th>
<th>Note</th>
<th data-hide="all">Live</th>
<th>Action</th>
{{-- <th>Delete</th> --}}
</tr>
</thead>
<tbody>
#foreach($links as $link)
<tr>
<td align="center"><input type="checkbox" class="i-checks" id="input" name="input[]" value="{{ $link->page_url }}"></td>
<td>{{ $link->company->name }}</td>
{{-- <td>{{ $link->category_id }}</td> --}}
<td>{{ $link->page_url }}</td>
<td>{{ $link->domain_url }}</td>
<td>{{ $link->destination_url }}</td>
<td>{{ $link->contact_email }}</td>
<td>{{ $link->trust_flow}}</td>
<td>{{ $link->estimated_traffic }}</td>
<td>{{ $link->domain_authority }}</td>
<td>{{ $link->status }}</td>
<td>{{ $link->comment }}</td>
<td>
#if($link->is_live)
{{ 'Live' }}
#else
{{ 'Down' }}
#endif
</td>
<td>
<a data-toggle="modal" href="#modal-form-{{ $link->id }}" data-dismiss="modal"><i class="fa fa-pencil"></i> Edit</a>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</form>
modal.blade.php
<div id="myModal" class="modal inmodal fade" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-body text-center">
<i class="fa fa-exclamation-triangle modal-icon"></i>
<h2><strong>Are you sure?</strong></h2>
{{-- <p class="text-muted">Are you sure you want to delete the following link(s)?</p> --}}
<p id="checkid"></p>
<div class="row">
<button id="cancelDelete" type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button class="btn-primary btn" id="SubForm">Confirm and Submit The Form</button>
</div>
</div>
</div>
</div>
javascript
$("#linkForm").validate({
submitHandler: function (form) {
$("#myModal").modal('show');
$('#SubForm').click(function () {
form.submit();
});
}
});
How can I submit my array to the controller without creating another function and route for multiple delete and just use links.destroy?
You can pass the array from the form like this
<input type="checkbox" name="ids[]" value="{{$row->id}}"/>
In your controller you can do
Model::whereIn('id', $request->ids)->destroy();
You can always collect the links through JS and send them along. In the Laravel controller you'll simply add an if condition to differentiate between the two requests.
By the by, whether it is only one id or several, you should always make sure that permissions over them are enforced.
Elaborating on my answer,
Suppose you've got a collection of articles, each of which can be marked for deletion
<input type="checkbox" name="article[]" value="1" checked>
<input type="checkbox" name="article[]" value="2" checked>
<input type="checkbox" name="article[]" value="3" checked>
<input type="checkbox" name="article[]" value="4">
Then you can collect those ids with
let ids = Array.from(document.querySelectorAll('input[name="article[]"'))
.filter(el => el.checked)
.map(el => el.value);
And later on send them thorugh an XMLHttpRequest