Seeders and factories

Contents
A seeder class
Factory states
Run them
Seed dev data

A seeder class

A seeder is a plain class with a run method that writes rows into the database. Seeders live in database/seeders and usually call create on one model at a time.

Keep each seeder focused on one table so sample data stays easy to read and easy to rerun from scratch.

use App\Models\User; use Illuminate\Database\Seeder; class UserSeeder extends Seeder { public function run(): void { User::create([ "name" => "Demo User", "email" => "demo@example.com", ]); } }

Factory states

Factories generate fake models in bulk when one hand written row is not enough. The default factory knows valid values for every column, and a state tweaks one detail such as an unverified address.

Combine a count with a state to build exactly the data set a feature needs for a manual test.

use App\Models\User; User::factory()->create([ "name" => "Demo User", ]); User::factory()->count(10)->create(); User::factory()->unverified()->create();

Run them

Generate a new seeder file with the canonical maker command, then run all seeders with the seed command. The first command prints the path of the created file.

php artisan make:seeder UserSeeder

INFO Seeder created successfully.

Run the whole set any time the local database needs a fresh start:

php artisan db:seed

INFO Seeding database.

Seed dev data

The main DatabaseSeeder calls the smaller seeders in order, so one command seeds everything. List only development helpers there and keep production data out of the file.

Real projects often guard extra demo content behind a local environment check so staging and production stay clean.

use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { public function run(): void { $this->call([ UserSeeder::class, ]); } }

Next: Validation

Article author: Arthur Isaev

Related articles
Development with PHP
What is Laravel and where it runs
Install Laravel
Your first route
Controllers
Blade basics
Blade advanced
Migrations
Eloquent models
Relationships
Query builder
Seeders and factories
Validation
Auth intro
Hand-rolled login
Middleware
Service container
Config and env
Artisan commands
Events and listeners
Queues
Mail
Cache
Sessions in Laravel
CSRF
Hashing
Sanctum tokens
API resources
Gates and policies
Notifications
Scheduler
Filesystem
Localization
Testing
HTTP tests
Collections
Carbon dates
Pagination
File uploads
Eloquent advanced
Transactions and locks
Deployment
Debugbar and dd
Broadcasting
Full-text search
Capstone: blog
Capstone: JSON API
Capstone: shop cart
Performance: kill N+1
Modern Laravel tour
Hero roadmap: the whole course on one page
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: