Hướng dẫn export excel laravel from view - xuất excel laravel từ chế độ xem

Xuất khẩu có thể được tạo ra từ chế độ xem Blade, bằng cách sử dụng mối quan tâm FromView.

namespace App\Exports;

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

class InvoicesExport implements FromView
{
    public function view(): View
    {
        return view('exports.invoices', [
            'invoices' => Invoice::all()
        ]);
    }
}

123456789101112131415
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Nó sẽ chuyển đổi một bảng HTML thành bảng tính Excel.Ví dụ;________số 8:

<table>
    <thead>
    <tr>
        <th>Nameth>
        <th>Emailth>
    tr>
    thead>
    <tbody>
    @foreach($invoices as $invoice)
        <tr>
            <td>{{ $invoice->name }}td>
            <td>{{ $invoice->email }}td>
        tr>
    @endforeach
    tbody>
table>

12345678910111213141516
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Bạn có thể tải xuống xuất khẩu trong bộ điều khiển của mình:

public function export() 
{
    return Excel::download(new InvoicesExport, 'invoices.xlsx');
}

1234
2
3
4

Tôi đã cố gắng thực hiện xuất sang tệp Excel từ phương pháp xem bằng cách sử dụng Laravel Excel.Dưới đây là liên kết của tài liệu https://laravel-excel.maatwebsite.nl/3.1/exports/from-view.html.Nhưng tôi không thể tìm ra nó chưa tham khảo ví dụ được hiển thị trong trang web.Nó trả về một lỗi cho biết PhpOffice \ PhpSpreadsheet \ Writer \ Exception Invalid parameters passed..Tôi đã thay đổi mã của mình khi cố gắng giải quyết vấn đề này nhưng không có may mắn nào cả.Xin hãy giúp tôi tìm ra điều này.Dưới đây là mã của tôi.Cảm ơn bạn.

LoansExport.php

 Loan::all()
    ]);
 }
}

view_loan_export.blade.php









   @foreach ($loans as $loan)
        
    @endforeach
    
First Name Last Name
{{ $loan->member->fname }} {{ $loan->member->lname }}

LoansController.php

web.php

Route::get('/loanexport', 'LoansController@loanexport');

lỗi

Hướng dẫn export excel laravel from view - xuất excel laravel từ chế độ xem