PHP code example of iqbalatma / laravel-export-import
1. Go to this page and download the library: Download iqbalatma/laravel-export-import 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/ */
iqbalatma / laravel-export-import example snippets
use Iqbalatma\LaravelExportImport\Models\Export;
use Iqbalatma\LaravelExportImport\Models\Import;
use Iqbalatma\LaravelExportImport\Models\User;
return [
/*
|--------------------------------------------------------------------------
| Model Configuration
|--------------------------------------------------------------------------
|
| Here you may define the models that will be used by the package.
| This allows you to override the default models with your own
| implementations if necessary.
|
*/
"models" => [
"user" => User::class,
"import" => Import::class,
"export" => Export::class,
],
/*
|--------------------------------------------------------------------------
| File Paths
|--------------------------------------------------------------------------
|
| Define the directories used for storing export and import files.
| Temporary files will be generated during processing before being
| moved to the final disk storage.
|
*/
"path" => [
"export_path" => "exports",
"import_path" => "imports",
"temporary" => "tmp",
],
/*
|--------------------------------------------------------------------------
| Import Disk
|--------------------------------------------------------------------------
|
| The disk that will be used to store uploaded import files.
| This should correspond to one of the disks defined in the
| "filesystems" configuration file.
|
*/
"import_disk" => env("EXPORT_IMPORT_IMPORT_DISK", "s3"),
/*
|--------------------------------------------------------------------------
| Export Disk
|--------------------------------------------------------------------------
|
| The disk where generated export files will be stored.
| You may configure this to use local, s3, or any supported
| filesystem disk.
|
*/
"export_disk" => env("EXPORT_IMPORT_EXPORT_DISK", "s3"),
/*
|--------------------------------------------------------------------------
| Export Availability (Hours)
|--------------------------------------------------------------------------
|
| Determines how long exported files remain available for download
| before they expire. The value is defined in hours.
|
*/
"export_available_until" => 72,
/*
|--------------------------------------------------------------------------
| Job Default Timeout (Seconds)
|--------------------------------------------------------------------------
|
| Determines how long job timeout. This value is defined in seconds
|
*/
"job_timeout" => 1200,
];
use \Iqbalatma\LaravelExportImport\Traits\InteractWithExport
namespace App\Services;
use App\Jobs\Exports\ExportUserJob;
use \Iqbalatma\LaravelExportImport\Traits\InteractWithExport
use Illuminate\Contracts\Container\BindingResolutionException;
class UserService
{
use InteractWithExport;
/**
* @param array $requestedData
* @return bool
* @throws BindingResolutionException|AdmissionException|\Throwable
*/
public static function handle(array $requestedData): bool
{
$service = new static();
$service->createExportEntity(
exportType: "USER",
exportName: "Export User"
permissionName: "can.access.user",
callback: function () {
ExportUserJob::dispatch($service->export);
}
);
return true;
}
}
namespace App\Jobs;
use App\Models\User;
use Illuminate\Support\LazyCollection;
use Iqbalatma\LaravelExportImport\Abstracts\BaseExportJob;
class ExportUserJob extends BaseExportJob
{
protected LazyCollection $users;
protected array $header = [
'name',
'email'
];
protected function executeQuery(): self
{
$this->users = User::query()->lazy(200);
return $this;
}
protected function writeFile(): self
{
foreach ($this->users as $user) {
fputcsv($this->file, [
$user->name,
$user->email
]);
}
return $this;
}
}
protected function afterExport(): void
{
// Example: send notification
}
use \Iqbalatma\LaravelExportImport\Traits\InteractWithImport
namespace App\Services;
use App\Jobs\Exports\ImportUserJob;
use \Iqbalatma\LaravelExportImport\Traits\InteractWithImport
use Illuminate\Contracts\Container\BindingResolutionException;
class UserService
{
use InteractWithImport;
/**
* @param array $requestedData
* @return bool
* @throws BindingResolutionException|AdmissionException|\Throwable
*/
public static function handle(array $requestedData): bool
{
$service = new static();
$file = request()?->file("file");
if (!$file) {
throw ValidationException::withMessages(["file" => "Required file not found"]);
}
$service->createImportEntity(
file: $file,
importType: "USER",
importName: "Import User"
permissionName: "can.access.user",
callback: function () {
ImportUserJob::dispatch($service->import);
}
);
return true;
}
}
namespace App\Jobs;
use App\Models\User;
use Illuminate\Support\LazyCollection;
use Iqbalatma\LaravelExportImport\Abstracts\BaseImportJob;
class ImportUserJob extends BaseImportJob
{
protected function readFile(): self
{
$this->getLazyCollection()
->chunk(200)
->each(function (LazyCollection $collection) {
foreach ($collection as $row) {
try {
DB::beginTransaction();
#get data from db
/** @var User $userFromDB */
$userFromDB = User::query()->create($row);
$this->successRow++;
DB::commit();
} catch (Exception $e) {
DB::rollBack();
$this->generateFileError()
->writeErrorRow($row, $e->getMessage());
$this->failedRow++;
} finally {
$this->totalRow++;
}
}
});
return $this;
}
}
bash
php artisan vendor:publish --tag=migrations
bash
php artisan migrate
config/filesystems.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.