I am working with Laravel 5.8 with PHP 7.4 to develop my project, and in this project, I wanted to make an Excel file out of a table called students
from View.
So here is what I did:
I typed composer require maatwebsite/excel
at the Terminal and downloaded the package.
I added \Maatwebsite\Excel\ExcelServiceProvider::class,
in providers
at config/app.php
, and 'Excel' => \Maatwebsite\Excel\Facades\Excel::class,
as aliases
.
And then made this new Export file:
php artisan make:export StudentExportView --model=App\Member\Student
And this file holds this:
namespace App\Exports;
use App\Member\Student;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FormView;
use Maatwebsite\Excel\Facades\Excel;
class StudentExportView implements FormView
{
/**
* @return \Illuminate\Support\Collection
*/
public function view(): View
{
return view('admin.students.custom', [
'customs' => Student::all()
]);
}
}
And at the Controller, I have added this method:
public function export_view()
{
return Excel::download(new StudentExportView, 'students.xlsx');
}
And also these routes to web.php
:
Route::get('export_view','StudentAdminController@export_view')->name('students.export_view');
But when I it out, I get this error:
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Interface 'App\Exports\FormView' not found
So what's going wrong here?
Note
If I remove use Maatwebsite\Excel\Concerns\FormView;
from Export Class, an empty excel file downloads
And the package is "maatwebsite/excel": "^3.1"