PHP code example of kstmostofa / laravel-backfill

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

    

kstmostofa / laravel-backfill example snippets


namespace App\Backfills;

use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
use Kstmostofa\Backfill\Backfill;

class BackfillUserSlugs extends Backfill
{
    public int $batchSize = 1000;

    public int $sleepMs = 100;

    public function collection(): Builder
    {
        return User::query()->whereNull('slug');
    }

    public function process($record): void
    {
        $record->update(['slug' => Str::slug($record->name)]);
    }
}

// config/backfill.php
'dashboard' => ['enabled' => true, 'path' => 'backfills', 'middleware' => ['web']],

use Kstmostofa\Backfill\Dashboard\Dashboard;

Dashboard::auth(fn ($request) => $request->user()?->isAdmin() === true);

class BackfillOrderRefunds extends Backfill
{
    public bool $operatorRunnable = true;

    public function description(): string
    {
        return 'Re-issue refund receipts';
    }

    public function parameters(): array
    {
        return [
            Parameter::ids('order_ids', 'Order IDs')
                ->)
            ->whereIn('id', $this->parameter('order_ids', []));
    }
}

Dashboard::operatorAuth(fn ($request) => $request->user()?->isSupport() === true);

public bool $ledger = true;
public bool $externalSideEffects = true;

public function tenants(): ?iterable
{
    return Tenant::query()->pluck('id');
}

public function useTenant(string|int $tenant): void
{
    Tenant::find($tenant)->makeCurrent();
}

'notifications' => [
    'enabled' => true,
    'on' => ['completed', 'failed', 'paused'],
    'mail' => '[email protected]',
    'slack_webhook' => 'https://hooks.slack.com/services/...',
],

use Kstmostofa\Backfill\Testing\InteractsWithBackfills;

uses(InteractsWithBackfills::class);

it('slugs every user', function () {
    User::factory()->count(5)->create(['slug' => null]);

    $run = $this->runBackfill(BackfillUserSlugs::class);

    expect($run->processed_count)->toBe(5)
        ->and(User::whereNull('slug')->count())->toBe(0);
});
bash
php artisan vendor:publish --tag=backfill-migrations
php artisan vendor:publish --tag=backfill-config
bash
php artisan make:backfill BackfillUserSlugs
bash
php artisan backfill:run user-slugs --queue
bash
php artisan backfill:run order-refunds --param=order_ids=1,2,3 --param=tone=friendly