1

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 enter image description here

0

2 Answers 2

4

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>
Sign up to request clarification or add additional context in comments.

1 Comment

You've made my day lighter
2

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.