Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Installation
- ```
- composer require maatwebsite/excel
- ```
- After updating composer, add the ServiceProvider to the providers array in `config/app.php`
- ```php
- Maatwebsite\Excel\ExcelServiceProvider::class,
- ```
- You can use the facade for shorter code. Add this to your aliases:
- ```php
- 'Excel' => Maatwebsite\Excel\Facades\Excel::class,
- ```
- The class is bound to the ioC as excel
- ```php
- $excel = App::make('excel');
- ```
- To publish the config settings in Laravel 5 use:
- ```
- php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
- ```
- This will add an `excel.php` config file to your config folder.
- # Usage
- Below is a simplest way to get the user list and export it to Excel, then you automatically download the Excel file.
- ```php
- Route::get('/phpexcel', function () {
- Excel::create('New file', function ($excel) {
- $excel->sheet('New sheet', function ($sheet) {
- $users = App\User::all();
- $sheet->loadView('exports.excel.users.index', compact('users'));
- });
- })->download('xls');
- });
- ```
- ## The View
- ```html
- <!DOCTYPE html>
- <html>
- <head>
- <title>User List</title>
- </head>
- <body>
- <table>
- <thead>
- <th>Name</th>
- <th>E-mail</th>
- </thead>
- <tbody>
- @foreach($users as $user)
- <tr>
- <td>{{ $user->name }}</td>
- <td>{{ $user->email }}</td>
- </tr>
- @endforeach
- </tbody>
- </table>
- </body>
- </html>
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement