PHP code example of ntoufoudis / hopper

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

    

ntoufoudis / hopper example snippets


use Ntoufoudis\Hopper\Hopper;
use Ntoufoudis\Hopper\Sources\CsvSource;

$run = Hopper::define(CustomerImport::class)
    ->from(CsvSource::make(storage_path('app/imports/customers.csv')))
    ->autoMap()
    ->stage();

$preview = $run->preview();   // exact, free pre-commit counts
// $preview->total, ->valid, ->errors, ->inserts, ->updates, ->skips

$run->commit();               // replays staged rows into the target model

use Ntoufoudis\Hopper\Contracts\Resolver;
use Ntoufoudis\Hopper\ImportDefinition;
use Ntoufoudis\Hopper\Resolution\UpsertResolver;

final class CustomerImport extends ImportDefinition
{
    public function model(): string
    {
        return Customer::class;
    }

    public function resolver(): Resolver
    {
        return UpsertResolver::by('email');
    }

    public function rules(): array
    {
        return ['email' => ['

use Ntoufoudis\Hopper\Export\FailedRowExporter;

$csv = app(FailedRowExporter::class)->export($run);

'audit' => ['driver' => 'chronicle'],

  use Illuminate\Support\Facades\Storage;
  use Ntoufoudis\Hopper\Hopper;
  use Ntoufoudis\Hopper\Sources\CsvSource;

  $path = $request->file('import')->store('imports');   // durable disk
  $run = Hopper::define(CustomerImport::class)
      ->from(CsvSource::make(Storage::path($path)))      // worker-readable path
      ->stage();                                         // safe to queue
  
bash
php artisan vendor:publish --tag=hopper-config
bash
php artisan migrate