glamorous / laravel-data-loader
Small package to provide tools for data migrations and loading initial data into your database
Requires
- php: ^8
- illuminate/contracts: ^10.0||^11.0
Requires (Dev)
- laravel/pint: ^1.14
This package is auto-updated.
Last update: 2025-01-08 11:32:12 UTC
README
Small package to provide tools for data migrations and loading initial data into your database.
Installation
You can install the package via composer:
composer require glamorous/laravel-data-loader
You should publish the config file with:
php artisan vendor:publish --tag="laravel-data-loader-config"
This is the contents of the published config file:
return [ 'loaders' => [ // EnsureSomeDataIsPresent::class, ], ];
Usage
Provide a list of loaders in config/data-loader.php
with their class names. Those classes should implement the DataLoader
-interface.
Example data loader class
<?php 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'); } }
Calling the loader
The command can be run after the migrations if you put the following in the boot of a Service Provider:
Event::listen(MigrationsEnded::class, function() { Artisan::call(DataLoaderCommand::class); });
Or you can just call the command in your CI-scripts:
php artisan data-loader:run
If necessary you can run the command with the --force
flag. This way it would not check if it data needs to be loaded or not.
Contributing
Package is open for pull requests!
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.