dmt-software / laravel-import-reader
import reader that can handles huge imports
Requires
Requires (Dev)
- laravel/legacy-factories: ^1.0.4
- orchestra/testbench: ^6.25
This package is auto-updated.
Last update: 2024-11-29 14:07:52 UTC
README
Laravel bridge for dmt-software/import-reader
to iterate over the contents of
(huge) imports.
Support
This package is tested on and requires laravel >= 8.0.
Installation
composer require dmt-software/laravel-import-reader
The service provider can be automatically registered by auto-discovery. Otherwise, register it manually add it to config/app.php:
'providers' => [ // ... DMT\Laravel\Import\Reader\Providers\ImportReaderServiceProvider::class, ];
To publish the configuration file:
php artisan vendor:publish --provider="DMT\Laravel\Import\Reader\Providers\ImportReaderServiceProvider"
Configuration
The configuration has entries for custom sanitizers, handlers and a custom
error handler. See the config/reader.php file for their usage or the
dmt-software/import-reader
documentation.
Usage
A file called items.json contains the following data:
[ { "id": 1, "name": "item-name", // ... }, { "id": 2, "name": "item2-name", // ... }, // ... ]
That can be read into chunks that can be imported into a database:
use DMT\Laravel\Import\Reader\Facades\ImportReader; /** @var iterable<int, array> $items */ $items = ImportReader::buildToArrayReader( 'directory-to/items.json', [ 'path' => '.' ])->read(); foreach ($items as $row => $item) { // process the items if (!empty($item['name'])) { Item::updateOrCreate($item); } }
Or the reader builder can be injected into your classes or methods.
use DMT\Import\Reader\ReaderBuilder; use Illuminate\Console\Command; class MyImportCommand extends Command { protected $signature = 'import:items {file}'; public function handle(ReaderBuilder $builder) { $reader = $builder->build($this->argument('file'), []); foreach ($reader->read($this->argument('skip')) as $item) { // process item } } }