PHP code example of umutcangungormus / laravel-import-export

1. Go to this page and download the library: Download umutcangungormus/laravel-import-export library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

umutcangungormus / laravel-import-export example snippets


use Illuminate\Database\Eloquent\Model;
use Umutcangungormus\LaravelImportExport\Contracts\Exportable;
use Umutcangungormus\LaravelImportExport\Contracts\Importable;
use Umutcangungormus\LaravelImportExport\Support\HasImportExport;

class Product extends Model implements Importable, Exportable
{
    use HasImportExport; // config-driven field resolution — nothing else needed
}

'models' => [
    App\Models\Product::class => [
        'unique_by'   => ['sku'],                              // updateOrCreate key
        'processor'   => App\Imports\ProductProcessor::class,  // optional
        'fields' => [
            'sku'   => ['elds' => [
            'id'    => ['accessor' => 'id'],
            'sku'   => ['accessor' => 'sku'],
            'name'  => ['accessor' => 'name'],
            'price' => ['accessor' => 'price'],
        ],
    ],
],

use Umutcangungormus\LaravelImportExport\Actions\ImportExport\InitializeImportAction;
use Umutcangungormus\LaravelImportExport\Actions\ImportExport\StartImportAction;
use Umutcangungormus\LaravelImportExport\Data\InitializeImportData;

// Upload → detect headers → auto-match columns
$session = app(InitializeImportAction::class)->handle(new InitializeImportData(
    model: App\Models\Product::class,
    file:  $request->file('file'),
));

// (optionally let the user adjust $session->mappings here, then…)

// Dispatch the queued batch pipeline
app(StartImportAction::class)->handle($session);

'address' => ['

use Umutcangungormus\LaravelImportExport\Contracts\ImportProcessorInterface;
use Umutcangungormus\LaravelImportExport\Models\ImportSession;

class ProductProcessor implements ImportProcessorInterface
{
    /** Transform a raw row before the model is saved. */
    public function prepare(ImportSession $session, array $data): array
    {
        $data['price'] = (float) str_replace(',', '.', $data['price'] ?? 0);

        return $data;
    }

    /** Hook after each row is persisted. */
    public function after(object $model, array $data): void
    {
        // e.g. attach tags, fire events…
    }

    /**
     * OPTIONAL, order-independent post-pass (called once, after every row).
     * Ideal for self-referential FKs (manager_id, parent_id, category trees)
     * whose targets may appear later in the file than the rows referencing them.
     */
    public function afterComplete(ImportSession $session): void
    {
        // resolve deferred relationships here
    }
}

use Illuminate\Database\Eloquent\Builder;
use Umutcangungormus\LaravelImportExport\Tenancy\TenantResolverContract;

class CompanyTenantResolver implements TenantResolverContract
{
    public function currentTenantId(): int|string|null
    {
        return auth()->user()?->company_id;
    }

    public function scopeQuery(Builder $query): Builder
    {
        return $query->where('company_id', $this->currentTenantId());
    }
}

// config/import-export.php
'tenancy' => ['resolver' => App\Support\CompanyTenantResolver::class],

Gate::define('import-export.session.create', fn ($user) => $user->can('manage-imports'));

use Umutcangungormus\LaravelImportExport\Services\ModelExportService;

// streamed CSV/XLSX download
return app(ModelExportService::class)->export(App\Models\Product::class);
bash
php artisan queue:batches-table
php artisan migrate