PHP code example of thomascombe / backpack-async-export

1. Go to this page and download the library: Download thomascombe/backpack-async-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/ */

    

thomascombe / backpack-async-export example snippets


return [
    'feature_enabled' => [
        'export' => true,
        'import' => true,
    ],
    'user_model' => 'App\Models\User',
    'import_export_model' => Thomascombe\BackpackAsyncExport\Models\ImportExport::class,
    'admin_export_route' => 'export',
    'admin_import_route' => 'import',
    'export_memory_limit' => '2048M',
    'disk' => 'local',
];

use Thomascombe\BackpackAsyncExport\Http\Controllers\Admin\Interfaces\ExportableCrud;

class {Name}CrudController extends CrudController implements ExportableCrud {}

use \Thomascombe\BackpackAsyncExport\Http\Controllers\Admin\Traits\HasExportButton;

public function setup()
{
    // ...
    $this->addExportButtons();
}

use Thomascombe\BackpackAsyncExport\Enums\ActionType;
use Thomascombe\BackpackAsyncExport\Enums\ImportExportStatus;
use Thomascombe\BackpackAsyncExport\Models\ImportExport;

public function getExport(): ImportExport
{
    return ImportExport::create([
        ImportExport::COLUMN_USER_ID => backpack_user()->id,
        ImportExport::COLUMN_ACTION_TYPE => ActionType::Export,
        ImportExport::COLUMN_STATUS => ImportExportStatus::Created,
        ImportExport::COLUMN_FILENAME => sprintf('export/users_%s.xlsx', now()->toIso8601String()),
        ImportExport::COLUMN_EXPORT_TYPE => UserExport::class,
    ]);
}

public function getExportParameters(): array
{
    return [];
}

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Thomascombe\BackpackAsyncExport\Exports\SimpleCsv;

class UserExport extends SimpleCsv
{
    public function query(): EloquentBuilder
    {
        return User::query();
    }

    public function headings(): array
    {
        return [
            'ID',
            'Name',
            'Email',
        ];
    }

    /**
     * @param User $user
     * @return array
     */
    public function map($user): array
    {
        return [
            $user->id,
            $user->name,
            $user->email,
        ];
    }
}


use Thomascombe\BackpackAsyncExport\Http\Controllers\Admin\Interfaces\ImportableCrud;

class {Name}CrudController extends CrudController implements ImportableCrud {}

use Thomascombe\BackpackAsyncExport\Http\Controllers\Admin\Traits\HasImportButton;

public function setup()
{
    // ...
    $this->addImportButtons();
}

use Thomascombe\BackpackAsyncExport\Enums\ActionType;
use Thomascombe\BackpackAsyncExport\Enums\ImportExportStatus;
use Thomascombe\BackpackAsyncExport\Models\ImportExport;

public function getImport(): ImportExport
{
    return ImportExport::create([
        ImportExport::COLUMN_USER_ID => backpack_user()->id,
        ImportExport::COLUMN_ACTION_TYPE => ActionType::Import->value,
        ImportExport::COLUMN_STATUS => ImportExportStatus::Created,
        ImportExport::COLUMN_FILENAME => '',
        ImportExport::COLUMN_EXPORT_TYPE => UserImport::class,
    ]);
}

public function getImportParameters(): array
{
    return [
        'private' => [
            'hint' => 'CSV file 

class ImportExport extends \Thomascombe\BackpackAsyncExport\Models\ImportExport
{
}


namespace App\Exports;

use Thomascombe\BackpackAsyncExport\Exports\ExportWithName;

class UserExport implements ExportWithName
{
    public static function getName(): string
    {
        return 'My export name';
    }
}

public function getAvailableExports(): array
{
    return [
        'default' => null,
        'all' => 'All',
    ];
}

public function getExport*All*(): ImportExport
{
    return ImportExport::create(...);
}

public function getExport*All*Parameters(): array
{
    return [...];
}
bash
php artisan vendor:publish --provider="Thomascombe\BackpackAsyncExport\BackpackAsyncExportServiceProvider" --tag="backpack-async-export-migrations"
php artisan migrate
bash
php artisan vendor:publish --provider="Thomascombe\BackpackAsyncExport\BackpackAsyncExportServiceProvider" --tag="backpack-async-export-config"
bash
php artisan backpack:add-sidebar-content "<li class='nav-item'><a class='nav-link' href='{{ backpack_url('export') }}'><i class='nav-icon la la-file-export'></i> <span>Export</span></a></li>"
# or 
php artisan backpack:add-menu-content "<x-backpack::menu-item title='Export' icon='la la-file-export' :link=\"backpack_url('export')\" />"
bash
php artisan make:export UserExport --model=App/Models/User
bash
php artisan backpack:crud {ModelName}
bash
php artisan backpack:crud {Name}CrudController