Laravel Excel Export - From View not working - laravel

I tried to implement exporting to excel file From View approach using the Laravel Excel. Here is the link of the documentation https://laravel-excel.maatwebsite.nl/3.1/exports/from-view.html. But I can't figure it out yet referencing the example shown in the website. It returns an error saying PhpOffice \ PhpSpreadsheet \ Writer \ Exception
Invalid parameters passed. . I've been changing my codes trying to solve this but no luck at all. Please help me figure this out. Below are my codes. Thank you.
LoansExport.php
<?php
namespace App\Exports;
use App\Loan;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
class LoansExport implements FromView
{
public function view(): View
{
return view('partials.view_loan_export', [
'loans' => Loan::all()
]);
}
}
view_loan_export.blade.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
#foreach ($loans as $loan)
<tr>
<td >{{ $loan->member->fname }}</td>
<td >{{ $loan->member->lname }}</td>
</tr>
#endforeach
</tbody>
</table>
</body>
LoansController.php
<?php
namespace App\Http\Controllers;
use App\Loan as Loan;
use App\Member as Member;
use Illuminate\Http\Request;
use App\Exports\LoansExport;
use Maatwebsite\Excel\Facades\Excel;
class LoansController extends Controller
{
public function loanexport()
{
return Excel::download(new LoansExport, 'loans.xlsx');
}
}
web.php
Route::get('/loanexport', 'LoansController#loanexport');
error

I have done By different way
LoansController.php
public function loanexport(){
$loan= array();
$loans= loan::all();
$data = [
'success' => 'success',
'loans' => $loans,
];
return Excel::download(new LoansExport($data), 'loans.xlsx');
}
LoansExport.php
public function __construct($data) {
$this->data = $data;
}
public function view(): View
{
//dd($this->data);
return view('partials.view_loan_export',$this->data);
}
view_loan_export.blade.php
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
#foreach ($loans as $loan)
<tr>
<td >{{ $loan->member->fname }}</td>
<td >{{ $loan->member->lname }}</td>
</tr>
#endforeach
</tbody>
</table>

just put the table tag and the tag within it in your view
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
#foreach ($loans as $loan)
<tr>
<td >{{ $loan->member->fname }}</td>
<td >{{ $loan->member->lname }}</td>
</tr>
#endforeach
</tbody>
</table>

Related

How to solve design break when use page-break in dompdf

When generating the pdf with page-break-before after 3rd rows the design is breaking down. Here is my code
<!DOCTYPE html>
<html lang="en">
<head>
<title>DOM-PDF</title>
</head>
<body>
<table style="width:100%;">
<thead>
<tr>
<th>Serial</th>
<th>Name</th>
<th>Roll</th>
<th>Info</th>
</tr>
</thead>
<tbody>
#php
$n = 0;
for($i=0;$i<10;$i++){
#endphp
<tr style="text-align:center;">
<td>1</td>
<td>Rashedul Hasan</td>
<td>1208039</td>
<td>I am an employee.</td>
</tr>
#php
if($n==3){
echo "<i style='page-break-before:always;'></i> ";
}
$n++;
}
#endphp
</tbody>
</table>
</body>
</html>
This is the pdf view of page 1
This is the pdf view of page 2
In page 2 second row is moving to the right. How to fix it?
What about using chunks instead of breaking the table?
{{-- Replace collect()->times(10) with a collection when you're getting the actual data from DB --}}
#foreach (collect()->times(10)->chunk(3) as $chunk)
<table style="width:100%;">
<thead>
<tr>
<th>Serial</th>
<th>Name</th>
<th>Roll</th>
<th>Info</th>
</tr>
</thead>
<tbody>
#foreach($chunk as $result)
<tr style="text-align:center;">
<td>{{ $result }}</td>
<td>Rashedul Hasan</td>
<td>1208039</td>
<td>I am an employee.</td>
</tr>
#endforeach
</tbody>
</table>
<i style='page-break-before:always;'></i>
#endforeach

Foreach loop has undefined variable in my view

The error is on the view which says that
my $audio variable is not defined.
I want to show everything from the database using a foreach loop.
I tried base on paginate on the laravel documentation https://laravel.com/docs/5.7/pagination#basic-usage
My controller is below:
use Illuminate\Http\Request;
use App\Audio_lectures;
use Illuminate\Support\Facades\DB;
public function index(){
$audio = DB::table('audio_lectures')->latest()->paginate();
return view('welcome', ['audio_lectures' => $audio]);
}
The welcome view is below:
<div class="container">
<div class="row">
<table class="table table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Subject</th>
<th scope="col">Date Added</th>
<th scope="col">Download</th>
</tr>
</thead>
<tbody>
#foreach ($audio as $audio_lectures)
<tr>
<th scope="row">{{$audio_lectures->id}}</th>
<td>{{$audio_lectures->subject}}</td>
<td>{{$audio_lectures->created_at}}</td>
<td>Download</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
My route web.php is below:
Route::get('/','Audio_lecturesController#index');
I expect to show all the audio lectures from my database to my view.
try this one
#foreach ($audio_lectures as $audio)
<tr>
<td scope="row">{{$audio->id}}</td>
<td>{{$audio->subject}}</td>
<td>{{$audio->created_at}}</td>
<td>
Download
</td>
</tr>
#endforeach
you are passing from controller audio_lectures array but you
getting as in your view audio
On your view file looping try this
#foreach ($audio_lectures as $value)
<tr>
<td scope="row">{{$value->id}}</td>
<td>{{$value->subject}}</td>
<td>{{$value->created_at}}</td>
<td>Download</td>
</tr>
#endforeach

Dompdf LoadView variable passing

i can't load able to pass a parameter to the dompdf function LoadView.
this is my controller:
public function pst_affluiti(){
$data=DB::all('Anagrafica')->get();
$pdf = PDF::loadView('output',['data' => $data]);
return $pdf->stream('output.pdf');
}
my output.blade.php
<body>
<div class="container">
<table class="table table-sm table-bordered" >
<thead>
<tr>
<th scope="col-sm-1 col-6">Data Affl.</th>
<th scope="col-sm-2">Cron.</th>
<th scope="col-sm-2">Cognome</th>
<th scope="col-sm-2">Nome</th>
<th scope="col-sm-2">Data nac.</th>
</tr>
</thead>
<tbody>
#foreach($data as $row)
<tr>
<td>{{$row->Cron}}</td>
<td>{{$row->Surname}}</td>
<td>{{$row->Name}}</td>
<td>{{$row->Date}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</body>
if i load a normal view of my query it works well.
Any suggest?
Thanks in advance
you can use
compact('data')
instead of
$data

laravel 5.2 relationship not showing

I have a Laravel 5.2 website and I am having trouble getting my relationship data to show. It was working fine before but I can't see my data. I am not getting any errors.
View
<table class="table table-striped">
<thead>
<tr>
<th>HAWB</th>
<th>Pallet Count</th>
<th>Piece Count</th>
<th>Weight</th>
<th>Status</th>
</tr>
</thead>
<tbody>
#foreach($cfs->hawbs as $hawb)
<tr>
<td>
<a data-toggle="collapse" data-target="#{{$hawb->hawb}}">
{{$hawb->hawb}}
</a>
</td>
<td>{{$cfs->hawb->pallet_ct}}</td>
<td>{{$hawb->piece_ct}}</td>
<td>{{$hawb->weight_no}} {{$hawb->weight_type}}</td>
<td></td>
<div id="{{$hawb->hawb}}" class="collapse">
Insert more info here
</div>
</tr>
#endforeach
</tbody>
</table>
Controller
public function getCfsStep(Request $request, $step)
{
$cfs = DB::table('cfs_deliveries');
$account = Account::pluck('name', 'id')->all();
return view('cfs.step_' . $step, compact('account', 'cfsDelivery'), ['cfs' => $request->session()->get('cfs')]);
}
CFSDelivery Model
public function hawbs()
{
return $this->hasMany('App\Hawb', 'cfs_delivery_id');
}
Hawb Model
public function cfsDelivery()
{
return $this->belongsTo('App\CfsDelivery');
}
Edit
I'm now getting this error:
Undefined property: Illuminate\Database\Query\Builder::$hawbs (View: /Applications/XAMPP/xamppfiles/htdocs/dsd-cms/resources/view‌​s/cfs/step_3.blade.p‌​hp

how to save data in word file from database in laravel

I want to create a word file as a report. I want to fetch all data from the table and put to the word file but I am not able to save data to the file. I am doing in laravel 5.2. File is getting created and downloaded but data which is coming from database is not getting saved. thanks everyone
Here is my controller code -
public function downloadAbleFile(Request $request){
//Fetching all records based on projectid
$project_id = $request->input('project_id');
$questiondetails = DB::table('questionnair')->where('project_id','=',$project_id)->get();
$headers = array(
"Content-type"=>"text/html",
"Content-Disposition"=>"attachment;Filename=report.doc"
);
$content = '<html>
<head>
<meta charset="utf-8">
</head>
<body>
<p>
<table>
<thead>
<tr>
<th>Project ID</th>
<th>Question Number</th>
<th>User ID</th>
<th>Answer</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
function project(){
foreach ($questiondetails as $question){
return(
echo "<tr>";
echo "<td>".$question->project_id."</td>";
echo "<td>".$question->question_num."</td>";
echo "<td>".$question->user_id."</td>";
echo "<td>".$question->answer."</td>";
echo "<td>".$question->status."</td>";
echo "</tr>";
);
}
}
project();
?>
</tbody>
</table>
</p>
</body>
</html>';
return Response::make($content,200, $headers);
}
Why do not you use blade?
Try this
in your controller:
public function downloadAbleFile(Request $request){
$project_id = $request->input('project_id');
$questiondetails = DB::table('questionnair')->where('project_id','=',$project_id)->get();
$headers = array(
"Content-type" => "text/html",
"Content-Disposition" => "attachment;Filename=report.doc"
);
$content = View::make('path.view', [
'questiondetails' => $questiondetails,
])->render();
return Response::make($content,200, $headers);
}
in your view
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<br>
<table>
<thead>
<tr>
<th>Project ID</th>
<th>Question Number</th>
<th>User ID</th>
<th>Answer</th>
<th>Status</th>
</tr>
</thead>
<tbody>
#foreach($questiondetails as $question)
<tr>
<td>{{ $question->project_id }}</td>
<td>{{ $question->question_num }}</td>
<td>{{ $question->user_id }}</td>
<td>{{ $question->answer }}</td>
<td>{{ $question->status }}</td>
</tr>
#endforeach
</tbody>
</table>
<br>
</body>
</html>

Resources