Download the PHP package laragear/populate without Composer
On this page you can find all versions of the php package laragear/populate. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download laragear/populate
More information about laragear/populate
Files in laragear/populate
Package populate
Short Description Populate your database with a supercharged, continuable seeder
License MIT
Informations about the package populate
Populate
Populate your database with a supercharged, continuable seeder.
Become a sponsor
Your support allows me to keep this package free, up-to-date and maintainable.
Requirements
- PHP 8.2
- Laravel 11 or later
Installation
You can install the package via Composer.
How does this work?
Laravel's Seeding system is very antique and basic. This library supercharges the seeding system of Laravel to make it more friendly to develop and run.
By hijacking the default Seeder with a better one, we can supercharge the seed system to allow per-step seeding, skipping and continuable seeding, without sacrificing on its normal features, and keeping compatibility with classic seeding classes.
Set up
You may create a super-seeder using make:super-seeder
Artisan command, and the name of the seeder.
You will receive a Super Seeder with single seed()
method, and two others one that will execute before and after.
[!TIP]
If you already created your application seeders, replace the
Illuminate\Database\Seeder
import toLaragear\Populate\Seeder
.
If you want to change the seeder stub, you may publish it through the vendor:publish
Artisan command under the stub
tag.
Usage
Instead of using the run()
method in your seeder, this library's seeders use the concept of Seed Steps. A Seed Step is just a public method that starts with seed
, or uses the Laragear\Populate\Attributes\SeedStep
attribute, named to briefly describe the records that are being inserted.
The container instantiates the Seeder and also calls each Seed Step, so you can use Dependency Injection as arguments anywhere you require.
For convenience, a Seed Step will persist all records if these return a Model Factory, a Collection of Models or a single Model.
When the seeder is called, each Seed Step will be output to the console like this:
php artisan db:seed
INFO Seeding database.
Database\Seeders\UserSeeder ...................................... RUNNING
~ Seed normal users ................................................. DONE
~ Seed banned users ................................................. DONE
Database\Seeders\UserSeeder .................................. 107 ms DONE
Custom Seed Step naming
Each Seed Step is described in the console output as First word capitalized, so a Seed Step function called seedVipUsers
will be displayed as Seed vip users
. If you total control, the SeedStep
attribute accepts an argument to change the output description.
That will output the step as named, verbatim:
php artisan db:seed
INFO Seeding database.
Database\Seeders\UserSeeder ...................................... RUNNING
~ Seed normal users ................................................. DONE
~ Seed non-authorized users ......................................... DONE
Database\Seeders\UserSeeder .................................. 107 ms DONE
Calling with arguments
When calling other seeders inside a Seed Step, you may use the usual $this->call()
method and its variants. Classic seeders will execute their run()
method as always if the method exists.
When doing calling a Super Seeder, you may set an array of arguments for each seed task by issuing their name as a key and the arguments as an array. This can be great when a seeder contains a Seed Steps that would require parameters to properly populate records on the database.
Before & After
When calling a seeder, you may implement the before()
and after()
methods to run logic before the Seed Steps are executed, and after all are done with, respectively. As with Seed Steps, these are called through the Service Container.
On Error
For better control on Seed Steps that returns errors, you may implement the onError()
method that receives the offending exception. It's great to use for cleaning artifacts before stopping the seeding operation.
You may also return or throw another exception to replace the previous exception.
Skipping
Laragear's Seeders support skipping either a Seed Step or the whole Seeder. Both are done through the skip()
method.
Skipping a Seed Step
To skip a Seed Step, you only need to call the skip()
method inside it.
It will output something like this:
php artisan db:seed
INFO Seeding database.
Database\Seeders\UserSeeder ...................................... RUNNING
~ Seed normal users ................................................. DONE
~ Seed non-authorized users ...................................... SKIPPED
Database\Seeders\UserSeeder .................................. 107 ms DONE
The skip()
method supports using a reason for skip which will be displayed in the console output.
Database\Seeders\UserSeeder ...................................... RUNNING
~ Seed normal users ................................................. DONE
~ Seed non-authorized users ...................................... SKIPPED
There are already banned users in the database
Database\Seeders\UserSeeder .................................. 107 ms DONE
[!TIP]
If transactions are not disabled, the
skip()
method will trigger a rollback wherever is called inside a Seed Step.
Skipping a Seeder
To skip a seeder completely, you may use the skip()
method on the before()
method, which runs before any Seed Step is executed. As with the Seed Steps, you may also include a skip reason.
Database\Seeders\CommentSeeder ................................... RUNNING
Database\Seeders\CommentSeeder ................................... SKIPPED
There are already comments in the database
Database\Seeders\CommentSeeder ................................. 0 ms DONE
Continue Seeding
Sometimes your Seeding command may fail for hard errors, leaving orphaned or incomplete records in your database and forcing you to seed the whole database again.
To avoid this, Laragear Populate allows a previous incomplete seeding to continue. Call the db:seed
command with the --continue
option, and if the seeding fails it will save the progress for the next attempt.
The seeding continuation is tied to the Seeder you call in the command, which by default is Database\Seeders\DatabaseSeeder
.
[!TIP]
When using the
--continue
options, transactions will be automatically turned on.
The console output will mark the seed step as CONTINUE
if the step it already ran.
php artisan db:seed
INFO Seeding database.
Database\Seeders\UserSeeder ...................................... RUNNING
~ Seed normal users ............................................. CONTINUE
~ Seed non-authorized users ......................................... DONE
Database\Seeders\UserSeeder ................................... 32 ms DONE
Recovering from Unique Constraints Violations
Sometimes a Seed Step may throw a Unique Constraints Violation exception, which happens when trying to insert a value that already exists on unique column, like primary keys. It's not too common, but it usually happens when a random generator mistakenly repeats a value, like emails or text.
If transactions are not disabled, the Populator will retry the Seed Step again, showing the retry on the console. If the error persists, it will be thrown.
php artisan db:seed
INFO Seeding database.
Database\Seeders\UserSeeder ...................................... RUNNING
~ Seed normal users ............................................. CONTINUE
~ Seed non-authorized users ................................. RETRY UNIQUE
~ Seed non-authorized users ......................................... DONE
Database\Seeders\UserSeeder ................................... 32 ms DONE
You can set the retryUnique
property of the SeedStep
attribute to any number of retries from the default 1
. Alternatively, setting it to false
or 0
will disable it.
Disable transactions
Laragear Populate's Seeders wraps each Seed Step into its own transaction using the default database connection. This means that, when a Seed Step fails, all database operations inside that method are rolled back.
If you want to disable transactions, you may use set the $usesTransactions
property to false
. In that case, if you require seed steps to be skipped, it's recommended to skip at the start of the method.
[!TIP]
Transactions use the
db:seed
command declared connection.
Injecting Factory instances
If you're injecting factory instances into a seed step, you will find that the configure()
method of the state won't be called since it's only set when using Model::factory()
or ModelFactory::new()
static methods.
If your Factory overrides the configure()
method, you're encouraged to manually instance the factory inside your seeder.
Laravel Octane compatibility
- There are no singletons using a stale app instance.
- There are no singletons using a stale config instance.
- There are no singletons using a stale request instance.
- There are no static properties written during a request.
There should be no problems using this package with Laravel Octane.
Security
If you discover any security related issues, issue a Security Advisor
License
This specific package version is licensed under the terms of the MIT License, at time of publishing.
Laravel is a Trademark of Taylor Otwell. Copyright © 2011-2025 Laravel LLC.
All versions of populate with dependencies
illuminate/console Version 11.*|12.*
illuminate/database Version 11.*|12.*
illuminate/filesystem Version 11.*|12.*
illuminate/pipeline Version 11.*|12.*
illuminate/support Version 11.*|12.*