PHP code example of softartisan / laravel-vanguard
1. Go to this page and download the library: Download softartisan/laravel-vanguard 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/ */
// app/Providers/AppServiceProvider.php
use App\Backup\CustomBackupManager;
use SoftArtisan\Vanguard\Services\BackupManager;
use SoftArtisan\Vanguard\Services\BackupStorageManager;
use SoftArtisan\Vanguard\Services\TenancyResolver;
use SoftArtisan\Vanguard\Services\Drivers\DatabaseDriver;
use SoftArtisan\Vanguard\Services\Drivers\StorageDriver;
public function register(): void
{
$this->app->bind(BackupManager::class, fn ($app) => new CustomBackupManager(
$app->make(DatabaseDriver::class),
$app->make(StorageDriver::class),
$app->make(BackupStorageManager::class),
$app->make(TenancyResolver::class),
));
}
namespace App\Backup;
use SoftArtisan\Vanguard\Models\BackupRecord;
use SoftArtisan\Vanguard\Services\BackupManager;
class CustomBackupManager extends BackupManager
{
public function backupTenant(mixed $tenant, array $options = []): BackupRecord
{
// Custom pre-backup hook
\Log::info('Starting custom backup for tenant', ['id' => $tenant->getTenantKey()]);
return parent::backupTenant($tenant, $options);
}
}
use App\Backup\EncryptedDatabaseDriver;
use SoftArtisan\Vanguard\Services\Drivers\DatabaseDriver;
$this->app->singleton(DatabaseDriver::class, EncryptedDatabaseDriver::class);
use App\Backup\CustomTenancyResolver;
use SoftArtisan\Vanguard\Services\TenancyResolver;
$this->app->singleton(TenancyResolver::class, CustomTenancyResolver::class);
use App\Backup\CustomVanguardScheduler;
use SoftArtisan\Vanguard\Console\VanguardScheduler;
$this->app->singleton(VanguardScheduler::class, CustomVanguardScheduler::class);
Schema::table('tenants', function (Blueprint $table) {
$table->string('vanguard_schedule')->nullable();
});
$tenant->update(['vanguard_schedule' => '0 3 * * 1']); // Every Monday at 03:00
namespace App\Backup;
use SoftArtisan\Vanguard\Services\TenancyResolver;
class CustomTenancyResolver extends TenancyResolver
{
public function tenantSchedule(mixed $tenant): ?string
{
// Example: honour the tenant's local timezone
$tz = $tenant->timezone ?? 'UTC';
$hour = (new \DateTime('02:00', new \DateTimeZone($tz)))
->setTimezone(new \DateTimeZone('UTC'))
->format('G');
return "0 {$hour} * * *";
}
}
namespace App\Backup;
use Illuminate\Console\Scheduling\Schedule;
use SoftArtisan\Vanguard\Console\VanguardScheduler;
class MultiScheduleVanguardScheduler extends VanguardScheduler
{
public function schedule(Schedule $schedule): void
{
if (! config('vanguard.schedule.enabled', true)) {
return;
}
$tz = config('vanguard.schedule.timezone', config('app.timezone', 'UTC'));
// ── Database-only landlord backup — every night at 02:00 ──────────────
$this->scheduleCommand(
$schedule,
'vanguard:backup --landlord --no-filesystem',
'0 2 * * *',
$tz,
);
// ── Full landlord backup (DB + filesystem) — Sundays at 03:00 ────────
$this->scheduleCommand(
$schedule,
'vanguard:backup --landlord',
'0 3 * * 0',
$tz,
);
// ── Per-tenant backups — keep the default per-tenant logic ────────────
if (config('vanguard.schedule.tenants', true) && $this->tenancy->isEnabled()) {
foreach ($this->tenancy->allTenants() as $tenant) {
$cron = $this->tenancy->tenantSchedule($tenant) ?? $this->globalCron();
$this->scheduleCommand(
$schedule,
"vanguard:backup --tenant={$tenant->getTenantKey()}",
$cron,
$tz,
);
}
}
// ── Pruning and tmp cleanup — inherited defaults ───────────────────────
if (config('vanguard.retention.enabled', true)) {
$schedule->command('vanguard:prune')
->daily()->timezone($tz)->withoutOverlapping()->runInBackground();
}
$schedule->command('vanguard:cleanup-tmp')
->hourly()->timezone($tz)->withoutOverlapping()->runInBackground();
}
}
use App\Backup\MultiScheduleVanguardScheduler;
use SoftArtisan\Vanguard\Console\VanguardScheduler;
$this->app->singleton(VanguardScheduler::class, MultiScheduleVanguardScheduler::class);