riddlestone / laravel-package-seeders
Provides tools for working with seeders in Laravel packages
Requires
- php: 8.2.*|8.3.*|8.4.*
- ext-json: *
- illuminate/container: ^11.0|^12.0
- illuminate/database: ^11.0|^12.0
- illuminate/support: ^11.0|^12.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.87
- orchestra/testbench: ^9.0|^10.0
- phpmd/phpmd: ^2.15
- phpunit/phpunit: ^11.5|^12.3
This package is not auto-updated.
Last update: 2025-09-23 06:35:19 UTC
README
This package provides tools to help with seeders in Laravel projects and packages.
Installation
Use composer to install this package:
composer require riddlestone/laravel-package-seeders
Usage
Auto Seeders
This package provides a way to have a single seeder run seeders from multiple different packages within your project.
Simply tag your seeders as AutoSeeder
s in a service provider, and then the
AutoSeeders
seeder will include them when it is run.
use Illuminate\Support\ServiceProvider;
use Riddlestone\LaravelPackageSeeders\Contracts\AutoSeeder;
class MyServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton(MySeeder::class);
$this->app->tag(MySeeder::class, AutoSeeder::class)
}
}
use Illuminate\Database\Seeder;
use Riddlestone\LaravelPackageSeeders\Seeders\AutoSeeders;
class DatabaseSeeder extends Seeder
{
public function run(): void
{
$this->call(AutoSeeders::class);
}
}
Versioned Data Seeders
Versioned data seeders let you insert, update, or delete (with SoftDeletes) data from a data set in your model. They are done using your model, so any mutators or casting in your model will be applied.
To extend and use this seeder you will need to provide the following:
- The model the data is for (
getModelClass()
) - The attributes to identify a row by (
getLookupAttributes()
) - The attribute to identify a version by (
getVersionAttribute()
), this defaults to "version" if not supplied - The data to update the database to (
getData()
)
For example,
use Riddlestone\LaravelPackageSeeders\Seeders\VersionedDataSeeder;
class ToySeeder extends VersionedDataSeeder
{
protected function getModelClass(): string
{
return Toy::class;
}
protected function getLookupAttributes(): array
{
return ['key'];
}
protected function getData() : array{
return [
[
'key' => 'spinning-top',
'version' => 1,
'name' => 'Spinning Top',
'color' => 'red',
],
[
'key' => 'pogo-stick',
'version' => 1,
'name' => 'Pogo Stick',
'color' => 'blue',
],
[
'key' => 'racing-car',
'version' => 1,
'name' => 'Racing Car',
'color' => 'red',
'deleted_at' => '2025-09-01 00:00:00',
],
];
}
}
Json Seeders
JsonSeeder
is an extension of VersionedDataSeeder
, which replaces
getData()
with getFilename()
. This allows you to separate the data from the
seeder by providing the path to a JSON file containing your data.
use Riddlestone\LaravelPackageSeeders\Seeders\JsonSeeder;
class ToySeeder extends JsonSeeder
{
protected function getModelClass(): string
{
return Toy::class;
}
protected function getLookupAttributes(): array
{
return ['key'];
}
protected function getFilename() : array{
return __DIR__ . '/toys.json';
}
}
[
{
"key": "spinning-top",
"version": 1,
"name": "Spinning Top",
"color": "red"
},
{
"key": "pogo-stick",
"version": 1,
"name": "Pogo Stick",
"color": "blue"
},
{
"key": "racing-car",
"version": 1,
"name": "Racing Car",
"color": "red",
"deleted_at": "2025-09-01 00:00:00"
}
]