Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. # Installation
  2.  
  3. ```
  4. composer require maatwebsite/excel
  5. ```
  6.  
  7. After updating composer, add the ServiceProvider to the providers array in `config/app.php`
  8.  
  9. ```php
  10. Maatwebsite\Excel\ExcelServiceProvider::class,
  11. ```
  12.  
  13. You can use the facade for shorter code. Add this to your aliases:
  14.  
  15. ```php
  16. 'Excel' => Maatwebsite\Excel\Facades\Excel::class,
  17. ```
  18.  
  19. The class is bound to the ioC as excel
  20. ```php
  21. $excel = App::make('excel');
  22. ```
  23.  
  24. To publish the config settings in Laravel 5 use:
  25.  
  26. ```
  27. php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
  28. ```
  29.  
  30. This will add an `excel.php` config file to your config folder.
  31.  
  32. # Usage
  33.  
  34. Below is a simplest way to get the user list and export it to Excel, then you automatically download the Excel file.
  35.  
  36. ```php
  37. Route::get('/phpexcel', function () {
  38. Excel::create('New file', function ($excel) {
  39. $excel->sheet('New sheet', function ($sheet) {
  40. $users = App\User::all();
  41. $sheet->loadView('exports.excel.users.index', compact('users'));
  42. });
  43. })->download('xls');
  44. });
  45. ```
  46.  
  47. ## The View
  48.  
  49. ```html
  50. <!DOCTYPE html>
  51. <html>
  52. <head>
  53. <title>User List</title>
  54. </head>
  55. <body>
  56. <table>
  57. <thead>
  58. <th>Name</th>
  59. <th>E-mail</th>
  60. </thead>
  61. <tbody>
  62. @foreach($users as $user)
  63. <tr>
  64. <td>{{ $user->name }}</td>
  65. <td>{{ $user->email }}</td>
  66. </tr>
  67. @endforeach
  68. </tbody>
  69. </table>
  70. </body>
  71. </html>
  72. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement