PDF generation using laravel - laravel

I am trying to generate a report in pdf format, I have a foreach loop which is looping though the objects, for some reason the data is not being displayed on the downloaded pdf.
any help is appreciated
here is my view page:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<table class="table table-striped">
<tr>
<th>Row No.</th>
<th>Entry ID</th>
<th>DC</th>
<th>Customer Name</th>
<th>ID No.</th>
<th>Referance No.</th>
<th>Entry Purpose</th>
<th>Time In</th>
<th>Time Out</th>
<th>Card No.</th>
<th>Cabinets</th>
<th>APC Card</th>
<th>Key</th>
</tr>
#foreach ($assignees as $assignee)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $assignee->id }}</td>
<td>{{ $assignee->datacenter }}</td>
<td>{{ $assignee->cust->idnumber }}</td>
<td>{{ $assignee->custidno}}</td>
<td>{{ $assignee->refnumber }}</td>
<td>{{ $assignee->entrypurpose}}</td>
<td>{{ $assignee->timein }}</td>
<td>{{ $assignee->timeout }}</td>
<td>{{ $assignee->cardno }}</td>
<td>{{ $assignee->cabinet }}</td>
<td>{{ $assignee->apccard }}</td>
<td>{{ $assignee->key }}</td>
</tr>
#endforeach
</table>
<div class="pull-right">
<strong> Report Generated on {{ date("d.m.Y") }} at {{date("h:i:sa")}} </strong>
</body>
</html>
here is my controller:
public function downloadPDF($id){
$assignees = assignee::latest();
$pdf = PDF::loadView('assignees.pdf', compact('assignees'));
return $pdf->download('invoice.pdf');
}

Instead of:
$assignees = assignee::latest();
Do:
$assignees = assignee::latest()->get()
Because latest() returns a QueryBuilder object not a collection, you have nothing to loopthough. You need to call get() to execute the query and return a result.
Also, slight nit-pick: classes (assignee) should be cammel case starting in a capital letter according to PSR-2 which Laravel follows.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
#if(count($assignees) > 0)
<table class="table table-striped">
<tr>
<th>Row No.</th>
<th>Entry ID</th>
<th>DC</th>
<th>Customer Name</th>
<th>ID No.</th>
<th>Referance No.</th>
<th>Entry Purpose</th>
<th>Time In</th>
<th>Time Out</th>
<th>Card No.</th>
<th>Cabinets</th>
<th>APC Card</th>
<th>Key</th>
</tr>
#foreach ($assignees as $assignee)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $assignee->id }}</td>
<td>{{ $assignee->datacenter }}</td>
<td>{{ $assignee->cust->idnumber }}</td>
<td>{{ $assignee->custidno}}</td>
<td>{{ $assignee->refnumber }}</td>
<td>{{ $assignee->entrypurpose}}</td>
<td>{{ $assignee->timein }}</td>
<td>{{ $assignee->timeout }}</td>
<td>{{ $assignee->cardno }}</td>
<td>{{ $assignee->cabinet }}</td>
<td>{{ $assignee->apccard }}</td>
<td>{{ $assignee->key }}</td>
</tr>
#endforeach
</table>
#else
<p>No data found!</p>
#endif
<div class="pull-right">
<strong> Report Generated on {{ date("d.m.Y") }} at {{date("h:i:sa")}} </strong>
</div>
</body>
</html>
Update the blade to this and check :
There is a closing </div> tag missing after the strong tag at the end
The $i is changed to $loop->index
I have added a condition to check if data exists otherwise show No data found!

Related

How to calculate total column value without query by Laravel or php?

Here above I want to Calculate Buying Total and selling Total in the current month . Suppose in November I want to calculate sell row total , buying row total and buying-selling total count. How Can I do that . Please help...
**<table id="datatable" class="table table-striped table-bordered">
<thead>
<tr>
<th>Date</th>
<th>Product Name</th>
<th>Quantity (Stock IN)</th>
<th>Buying Total</th>
<th>Stock</th>
<th>Selling Total</th>
<th>Profit (%)</th>
</tr>
</thead>
<tbody>
#foreach($product_all as $row)
<tr>
<td>{{ $row->buying_date}}</td>
<td>{{ $row->product_name}}</td>
<td>{{ $row->stock_in }}</td>
<td>{{ $row->buying_price*$row->stock_in }}</td> *********
<td>{{ $row->total_stock }}</td>
<td>{{ $row->selling_price*($row->stock_in-$row->total_stock)}}</td> ********
<td>{{ ($row->buying_price*$row->stock_in) - ($row->selling_price*($row->stock_in-$row->total_stock)) }}</td>
</tr>
#endforeach
</tbody>
</table>**
<h4 style="color: #ff3939; text-align: center;">November Total Invest : {{ #### }} <span> Sell : {{ #### }}</span></h4>
There are 2 ways to do:
First: setting parameters
**<table id="datatable" class="table table-striped table-bordered">
<thead>
<tr>
...
</tr>
</thead>
<tbody>
<?php
$yoursum1 = 0;
$yoursum2 = 0;
foreach($product_all as $row)
echo "<tr>
<td>{{ $row->buying_date}}</td>
<td>{{ $row->product_name}}</td>
<td>{{ $row->stock_in }}</td>
<td>{{ $row->buying_price*$row->stock_in }}</td> *********
<td>{{ $row->total_stock }}</td>
<td>{{ $row->selling_price*($row->stock_in-$row->total_stock)}}</td> ********
<td>{{ ($row->buying_price*$row->stock_in) - ($row->selling_price*($row->stock_in-$row->total_stock)) }}</td>
</tr>"
$yoursum1 += $yourvalue1;
$yoursum2 += $yourvalue2;
endforeach
echo "<tr>
<td></td>
<td>Summary</td>
<td>{{ $yoursum1}}</td>
<td>{{ $yoursum2}}</td> *********
<td>{{ $yoursum3}}</td>
<td>{{ $yoursum4}}</td> ********
<td>{{ $yoursum5 }}</td>
</tr>"
?>
</tbody>
</table>**
<h4 style="color: #ff3939; text-align: center;">November Total Invest : {{ #### }} <span> Sell : {{ #### }}</span></h4>
Second: using query to calculate and insert one row below
#foreach($product_all as $row)
<tr>
<td>{{ $row->buying_date}}</td>
<td>{{ $row->product_name}}</td>
<td>{{ $row->stock_in }}</td>
<td>{{ $row->buying_price*$row->stock_in }}</td> *********
<td>{{ $row->total_stock }}</td>
<td>{{ $row->selling_price*($row->stock_in-$row->total_stock)}}</td> ********
<td>{{ ($row->buying_price*$row->stock_in) - ($row->selling_price*($row->stock_in-$row->total_stock)) }}</td>
</tr>
#endforeach
<tr>
<td></td>
<td>Summary</td>
<td>{{ $value1 }}</td>
<td>{{ $value2 }}</td> *********
<td>{{ $value3 }}</td>
<td>{{ $value4 }}</td> ********
<td>{{ ($value5 }}</td>
</tr>
IT is advisable to keep calculations and processing out of the views. Controllers are meant to be the files where all calculations and data manipulations should be done.
//Say this is inside the controller method from which the view with data is returned as response
public function index(Request $request)
{
$product_all = Product::all();
$groupedByMonth = $product_all->groupBy(function($product) {
return \Illuminate\Support\Carbon::parse($product->date)->format('m');
});
$totals = (object) [
'buying' => $groupedByMonth->map(function($month){
return $month->sum('buying_total');
}),
'selling' => $groupedByMonth->map(function($month){
return $month->sum('selling_total');
}),
];
//Pass $totals to the view where it can be accessed as $totals->buying
}
As such the calculation/manipulation can be done in blade view between #php #endphp

Laravel Blade rendering table inside table with VueJS

I have this scenario working with Laravel blade view:
#foreach($event->drawingrequests as $drawing)
<tr>
<td>{{ $drawing->field1 }}</td>
<td>{{ $drawing->field2 }}</td>
<td>{{ $drawing->field3 }}</td>
<td>{{ $drawing->field4 }}</td>
<td>{{ $drawing->field5 }}</td>
<td>{{ $drawing->field6 }}</td>
<td>{{ $drawing->field7 }}</td>
</tr>
#if(count(drawing->childs) > 0)
<tr><td colspan="7">
<table class="table">
<tbody>
#foreach(drawing->childs as $stage)
<tr>
<td>{{ $stage->field1 }}</td>
<td>{{ $stage->field2 }}</td>
<td>{{ $stage->field3 }}</td>
<td>{{ $stage->field4 }}</td>
<td>{{ $stage->field5 }}</td>
</tr>
#endforeach
</tbody>
</table>
</td></tr>
#endif
#endforeach
I can I do it on VueJS??? The main question here in, on VueJs conditions are always connected to DOM elements.. I need to create other tr when childs > 0. And I need to check this condition on each drawing.
<tr v-for="drawing in eventdetail.event.drawingrequests">
<td>#{{ drawing.field1 }}</td>
<td>#{{ drawing.field2 }}</td>
<td>#{{ drawing.field3 }}</td>
<td>#{{ drawing.field4 }}</td>
<td>#{{ drawing.field5 }}</td>
<td>#{{ drawing.field6 }}</td>
<td>#{{ drawing.field7 }}</td>
</tr>
#if(drawing.childs > 0)
<tr>
<td colspan="7">
<table class="table">
<tbody>
<tr v-for="stage in drawing.childs">
<td>#{{ stage.field1 }}</td>
<td>#{{ stage.field2 }}</td>
<td>#{{ stage.field3 }}</td>
<td>#{{ stage.field4 }}</td>
<td>#{{ stage.field5 }}</td>
</tr>
</tbody>
</table>
</td>
</tr>
#endif
Found the solution:
<template v-for="drawing in eventdetail.event.drawingrequests">
<tr>
<td>#{{ drawing.field1 }}</td>
<td>#{{ drawing.field2 }}</td>
<td>#{{ drawing.field3 }}</td>
<td>#{{ drawing.field4 }}</td>
<td>#{{ drawing.field5 }}</td>
<td>#{{ drawing.field6 }}</td>
<td>#{{ drawing.field7 }}</td>
</tr>
<tr v-if="drawing.childs > 0">
<td colspan="7">
<table class="table">
<tbody>
<tr v-for="stage in drawing.childs">
<td>#{{ stage.field1 }}</td>
<td>#{{ stage.field2 }}</td>
<td>#{{ stage.field3 }}</td>
<td>#{{ stage.field4 }}</td>
<td>#{{ stage.field5 }}</td>
</tr>
</tbody>
</table>
</td>
</tr>
</template>
Reference link:
Vue: Displaying nested data in a table

Laravel - syntax error, unexpected ')', expecting '['

I am using Laravel 5.8 to create an App. When I wanted to display the view, it generated an error as shown below:
syntax error, unexpected ')', expecting '['
Controller
public function serviceOverview(Request $request)
{
$data['title'] = 'Subscription Overview';
$serviceoverviews = DB::table("service_package")
->join('services', 'services.id', '=', 'service_package.service_id')
->join('service_type', 'service_type.id', '=', 'services.service_type')
->select('service_package.title as service_id', 'service_package.title as package_name', DB::raw("DATE(service_package.created_at) as created_at"), 'service_package.price', 'service_package.days','services.name as service_name','service_type.name as service_type');
$render=[];
if(isset($request->package_name))
{
$serviceoverviews=$serviceoverviews->where('serviceoverviews','like','%'.$request->serviceoverviews.'%');
$render['package_name']=$request->package_name;
}
if(isset($request->service_id))
{
$serviceoverviews=$serviceoverviews->where('service_id',$request->service_id);
$render['service_id']=$request->service_id;
}
$serviceoverviews= $serviceoverviews->orderBy('created_at','DESC');
$serviceoverviews= $serviceoverviews->paginate(15);
$serviceoverviews= $serviceoverviews->appends($render);
$data['serviceoverviews'] = $serviceoverviews;
return view('report.serviceOverview',$data);
}
In the Controller, I tried to do some filtering. Also, I did raw query.
View
<div class="box box-primary">
<div class="box-header with-border">
#if(Session::has('flash_message'))
<div class="alert alert-success">
{{ Session::get('flash_message') }}
</div>
#endif
#if(count($detailsubscriptions))
<table class="table table-bordered table-hover table-striped table-condesed" id="commenter_info_table">
<caption></caption>
<thead>
<tr>
<td>#</td>
<td>Service</td>
<td>Package</td>
<td>Service Type</td>
<td>Date</td>
<td>Price</td>
<td>Days</td>
</tr>
</thead>
<tbody>
#foreach($serviceoverviews as $key => serviceoverview)
<tr>
<td>{{ ++$key }}</td>
<td>{{ serviceoverview->service_name }}</td>
<td>{{ $serviceoverview->package_name }}</td>
<td>{{ $serviceoverview->service_type }}</td>
<td>{{ serviceoverview->created_at }}</td>
<td>{{ $serviceoverview->price }}</td>
<td>{{ $serviceoverview->days }}</td>
</tr>
#endforeach
<tr>
<td colspan="8">
{{ $serviceoverview->links() }}
</td>
</tr>
</tbody>
</table>
#else
<div class="row text-center">
<h2>No Service Overview to show</h2>
</div>
#endif
</div>
</div>
I tried to check the code, but found nothing. How do I resolve this issue
You have missed a $ from the $serviceoverview variable in your blade #foreach. Change:
#foreach($serviceoverviews as $key => serviceoverview)
To
#foreach($serviceoverviews as $key => $serviceoverview)
Here is the full code updated with changes. Please refer to this
Controller.php
public function serviceOverview(Request $request)
{
$builder = DB::table("service_package")
->join('services', 'services.id', '=', 'service_package.service_id')
->join('service_type', 'service_type.id', '=', 'services.service_type')
->select('service_package.title as service_id', 'service_package.title as package_name', DB::raw("DATE(service_package.created_at) as created_at"), 'service_package.price', 'service_package.days','services.name as service_name','service_type.name as service_type');
if($request->filled('package_name'))) {
$builder = $builder->where('serviceoverviews','like','%'.$request->serviceoverviews.'%');
}
if(isset($request->service_id))
{
$builder=$builder->where('service_id',$request->service_id);
}
$serviceoverviews = $builder->orderBy('created_at','DESC')->paginate(15);
$data = [
'title' => 'Subscription Overview',
'serviceoverviews' => $serviceoverviews
];
return view('report.serviceOverview', $data);
}
Now change your view code like this.
view.blade.php
<table class="table table-bordered table-hover table-striped table-condesed" id="commenter_info_table">
<caption></caption>
<thead>
<tr>
<td>#</td>
<td>Service</td>
<td>Package</td>
<td>Service Type</td>
<td>Date</td>
<td>Price</td>
<td>Days</td>
</tr>
</thead>
<tbody>
#foreach($serviceoverviews as $key => $serviceoverview)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $serviceoverview->service_name }}</td>
<td>{{ $serviceoverview->package_name }}</td>
<td>{{ $serviceoverview->service_type }}</td>
<td>{{ serviceoverview->created_at }}</td>
<td>{{ $serviceoverview->price }}</td>
<td>{{ $serviceoverview->days }}</td>
</tr>
#endforeach
<tr>
<td colspan="8">
{{ $serviceoverview->appends(\Request::all())->links() }}
</td>
</tr>
</tbody>
</table>
You are using serviceoverview in place of $serviceoverview in view file. That would be an issue.
You have made some typo and one logical error.
Change
#foreach($serviceoverviews as $key => serviceoverview) to #foreach($serviceoverviews as $key => $serviceoverview)
<td>{{ serviceoverview->service_name }}</td> to <td>{{ $serviceoverview->service_name }}</td>
You have put {{ $serviceoverview->links() }} out side of the foreach loop

Laravel 5 unable to render pagination

I'm new to Laravel 5 and I have an issue displaying pagination. I looked in the documentation and just can't get it to work.
usercontroller:
public function getIndex()
{
$users = User::where('active', '=','verified')->simplePaginate(10);
return View::make('User.index')->with('users',$users);
}
index.blade.php:
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>id</th>
<th>username</th>
<th>email</th>
<th>role</th>
<th>created</th>
<th class="actions">Actions</th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->username }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->role }}</td>
<td>{{ $user->created_at }}</td>
<td class="actions">
blabla
</td>
</tr>
#endforeach
</tbody>
</table>
{!! $users->render() !!}
for some reason, it was printing an additional /
fix:
{!! str_replace('users/','users',$users->render()) !!}

Can't display blank table cells in laravel

I've got the following code which shows my clubs fixtures/results list for upcoming games:
<div class="row">
<div class="col-md-10 col-md-offset-1">
<span id="hide" class="first">1st XV Fixtures/Results</span><span id="show">2nd XV Fixtures/Results</span>
<table class="table table-bordered table-hover">
<tr>
<th>Date</th>
<th>Venue</th>
<th>Opposition</th>
<th>Kick Off</th>
<th>Type</th>
<th>Score</th>
<th>Result</th>
</tr>
#foreach ($fixtures as $fix)
#if($fix['team'] === 1)
<tr class="clickableRow success league_table" href="{{ action('DynamicPages#match',[$fix['id']]) }}">
<td>{{ date("d/m/Y",strtotime($fix['mdate'])) }}</td>
<td>{{ $fix['venue'] }}</td>
<td>{{ $fix['opposition'] }}</td>
<td>{{ date("H:i",strtotime($fix['mdate'])) }}</td>
<td>{{ $fix['type'] }}</td>
#foreach($results as $result)
#if($result['match_id']=== $fix['id'])
#if(!empty($result['score']))
<td>{{ $result['score'] }}</td>
<td>{{ $result['result'] }}</td>
#else
<td> </td>
<td> </td>
#endif
#endif
#endforeach
</tr>
#else
<tr class="clickableRow danger merit-table" style="display:none;" href="{{ action('DynamicPages#match',[$fix['id']]) }}">
<td>{{ date("d/m/Y",strtotime($fix['mdate'])) }}</td>
<td>{{ $fix['venue'] }}</td>
<td>{{ $fix['opposition'] }}</td>
<td>{{ date("H:i",strtotime($fix['mdate'])) }}</td>
<td>{{ $fix['type'] }}</td>
#foreach($results as $result)
#if($result['match_id']=== $fix['id'])
#if(isset($result['score']))
<td>{{ $result['score'] }}</td>
<td>{{ $result['result'] }}</td>
#else
<td> </td>
<td> </td>
#endif
#endif
#endforeach
</tr>
#endif
#endforeach
</table>
</div>
</div>
I want it to show the score from the DB, but if thats blank, then just to show a blank table with a background colour. However, it's simple showing nothing so when I look at it, it shows a white space.
Can anyone help?

Resources