dutchcodingcompany/laravel-seed-once

Simple package to allow running seeders just once.

1.0.0 2021-10-15 11:48 UTC

This package is auto-updated.

Last update: 2024-11-15 19:29:36 UTC


README

Latest Version on Packagist Total Downloads

This package provides a simple way to run seeders just once, even when called multiple times.

Installation

You can install the package via composer:

composer require dutchcodingcompany/laravel-seed-once

Usage

Marking a seeder to run just once is as simple as using the Once trait in you seeder.

<?php

namespace Database\Seeders;

use DutchCodingCompany\SeedOnce\Once;
use Illuminate\Database\Seeder;

class PermissionSeeder extends Seeder
{
    use Once;
    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run(): void
    {
        //
    }
}

Below is an example using a permission seeder and a user seeder which depends on the permission seeder.

# Seed permissions
Seeding: Database\Seeders\PermissionSeeder
Seeded:  Database\Seeders\PermissionSeeder (12.86ms)

# Seed users
# Because we want to make sure the permissions have been seeded
# call the permission seeder again in the user seeder.
Seeding: Database\Seeders\UserSeeder
Seeding: Database\Seeders\PermissionSeeder
Skipped: Database\Seeders\PermissionSeeder # Skipped
Seeded:  Database\Seeders\PermissionSeeder (0.01ms)
Seeded:  Database\Seeders\UserSeeder (12.76ms)

In case you need to know whether a seeder has run already you may use the provided seeded method.

if($this->seeded(PermissionSeeder::class)) {
    //
}

Testing

composer test

Credits

License

The MIT License (MIT). Please see License File for more information.