Import Handlers
To decouple your Excel-import code completely from the controller, you can use the import handlers.
class ExampleController extends Controller { public function importUserList(UserListImport $import) { // Handle the import $import->handleImport(); } }
The handleImport() method will dynamically call a handler class which is your class name appended with Handler
class UserListImportHandler implements \Maatwebsite\Excel\Files\ImportHandler {
public function handle(UserListImport $import) { // get the results $results = $import->get(); } }
Handling imported results
Getting all sheets and rows
After you have loaded a file, you can ->get() the results like so:
Excel::load('file.xls', function($reader) { })->get();
or
Excel::load('file.xls', function($reader) { // Getting all results $results = $reader->get(); // ->all() is a wrapper for ->get() and will work the same $results = $reader->all(); });
The ->get() and ->all() methods will return a sheet or row collection, depending on the amount of sheets the file has. You can disable this feature inside the import.php config by setting ‘force_sheets_collection’ to true. When set to true it will always return a sheet collection.
Table heading as attributes
By default the first row of the excel file will be used as attributes.
// Get the firstname
$row->firstname;
Note: by default these attributes will be converted to a slug. You can change the default inside the config excel::import.heading. Available options are: true|false|slugged|ascii|numeric|hashed|trans|original
True and slugged will be converted to ASCII as well when excel::import.to_ascii is set to true. You can change the default separator as well inside the config.
Comments