Import & Export Objects | Laravel Excel


本站和网页 https://docs.laravel-excel.com/3.1/architecture/objects.html 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

Import & Export Objects | Laravel Excel
Version LE 3.1 LE 3.0 LE 2.1 Nova 1.x
Commercial Support
Blog
(opens new window)
GitHub
(opens new window) Version LE 3.1 LE 3.0 LE 2.1 Nova 1.x
Commercial Support
Blog
(opens new window)
GitHub
(opens new window) Getting Started Architecture Concepts LifecycleImport & Export ObjectsConcernsExports Imports # Import & Export Objects Directory structureEncapsulationData transfer objectPlain Old PHP ObjectConstructorSettersGettersConcernsHooks The entire Laravel Excel philosophy evolves around having Export and/or Import objects. # Directory structure The location of these Import and Exports classes could be as follows: .
├── app
│   ├── Exports (Groups all exports in your app)
│   │   ├── UsersExport.php
│   │   ├── ProductsExport.php
│   │   └── Sheets (You can group sheets together)
│   │   ├── InactiveUsersSheet.php
│   │   └── ActiveUsersSheet.php
| |
│   ├── Imports (Groups all imports in your app)
│   │   ├── UsersImport.php
│   │   ├── ProductsImport.php
│   │   └── Sheets (You can group sheets together)
│   │   ├── OutOfStockProductsSheet.php
│   │   └── ProductsOnSaleSheet.php
│ 
└── composer.json
# Encapsulation These objects encapsulate the entire export or import process.
The idea behind it is that you can neatly use these objects in controllers, services, jobs, event listeners or commands.
In all of theses cases, the entire logic of how the export/import needs to happen is kept within this object. # Data transfer object The import/export objects are in essence simple data transfer objects (DTO).
This means they will transfer the information you want to export/import to the Excel writer/reader.
This information is not only the actual data you want to export, but also additional information like
the styling of the file, the name of the worksheet, the number format of the cell etc. In most cases, this means that your code doesn't need to have direct exposure to the actual read/write process. # Plain Old PHP Object Besides being a DTO, the import/export objects are also just Plain Old PHP Objects (POPO). This means that you can do anything with them that you can do with normal PHP objects. # Constructor For instance, you can simply inject data through the constructor of the export object. class UsersExport implements FromCollection {
private $year;
public function __construct(int $year)
$this->year = $year;
public function collection()
return Users::whereYear('created_at', $this->year)->get();
12345678910111213Excel::download(new UsersExport(2019), 'users.xlsx');
1# Setters Another option is to add setters for data that you want to pass. class UsersExport implements FromCollection {
private $year;
public function setYear(int $year)
$this->year = $year;
public function collection()
return Users::whereYear('created_at', $this->year)->get();
12345678910111213$export = new UsersExport();
$export->setYear(2019);
Excel::download($export, 'users.xlsx');
1234# Getters In similar fashion to setters, you can also add getters. This can potentially be useful if you want to keep a state of your export/import. For instance, you can keep a row count in your users import. namespace App\Imports;
use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
class UsersImport implements ToModel
private $rows = 0;
public function model(array $row)
++$this->rows;
return new User([
'name' => $row[0],
]);
public function getRowCount(): int
return $this->rows;
1234567891011121314151617181920212223After doing the import, we can request the state with the getter. $import = new UsersImport;
Excel::import($import, 'users.xlsx');
dd('Row count: ' . $import->getRowCount());
1234# Concerns Most of the export/import configuration is done by using Concerns. Concerns are basically just simple interfaces.
Implementing them will make the object adhere to a certain contract. This contract can request specific methods that e.g. data can be passed through.
In other cases it might not ask for any methods to be implemented, but merely functions as a pointer interface. Read more about Concerns in the concerns documentation. # Hooks In more complex cases you might want to hook into certain moments of the read/write process. This can be done by using Events.
To register these events in your import/export object, you need to implement the Maatwebsite\Excel\Concerns\WithEvents concern. namespace App\Exports;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\BeforeExport;
class UsersExport implements WithEvents
/**
* @return array
*/
public function registerEvents(): array
return [
// Handle by a closure.
BeforeExport::class => function(BeforeExport $event) {
// Do something before the export process starts.
},
];
1234567891011121314151617181920Alternatively, you can also configure these listeners globally if you want it to happen to any export. Writer::listen(BeforeExport::class, function () {
// Do something before any export process starts.
});
123 Help us improve this page! (opens new window)
Lifecycle
Concerns