PHP code example of glamorous / laravel-data-loader

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

    

glamorous / laravel-data-loader example snippets


return [
    'loaders' => [
        // EnsureSomeDataIsPresent::class,
    ],
];



namespace App\Database\State;

use App\Models\User;
use Glamorous\Database\DataLoader;

readonly final class EnsureSuperAdminIsPresent implements DataLoader
{
    public function __invoke(): void
    {
        $user = User::query()
            ->where('identifier', '=', $this->getSuperAdminIdentifier())
            ->firstOrNew();

        $user->identifier = $this->getSuperAdminIdentifier();
        $user->email = $this->getEmail();
        $user->password =  $this->getPassword();
        $user->name = 'Super Admin';

        $user->save();
    }

    public function shouldLoad(): bool
    {
        return !User::where('identifier', '=', $this->getSuperAdminIdentifier())->exists();
    }

    protected function getEmail(): string
    {
        return config('custom.superadmin.email');
    }

    protected function getPassword(): string
    {
        return Hash::make(config('custom.superadmin.password'))
    }

    protected function getSuperAdminIdentifier(): string
    {
        return config('custom.superadmin.identifier');
    }
}


Event::listen(MigrationsEnded::class, function() {
    Artisan::call(DataLoaderCommand::class);
});