3
votes

I would like to export an Excel file from a blade Laravel view using Laravel Excel but I got this error:

Declaration of App\Http\Controllers\ExportController::view($id): Illuminate\Contracts\View\View must be compatible with Maatwebsite\Excel\Concerns\FromView::view(): Illuminate\Contracts\View\View

Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use App\User;


class ExportController implements FromView
{

    public function view($id): View
    {
        return view('users.export', [
            'user' => User::find($id)
        ]);
    }
}

Route

Route::get('exportxls/{id}', 'ExportController@view');

Blade File

<table>My data is here...</table>

What is wrong ?

1

1 Answers

2
votes

Since you are implementing Maatwebsite\Excel\Concerns\FromView you should override view() without any parameters.

public function view(): View
{
    return view('users.export');
}

Since you need user_id, you could try to pass it to the function in the URL. Something like: server/exportxls?user_id=2

And then get it like:

$userId = request('user_id');

Check if this works for your case.