Repository navigation
Seeding
Laravex includes a database seeder and factory system for populating your database with test or development data. Seeders live in database/seeders/ and factories in database/factories/.
Seeders are plain Elixir modules with a run/0 function. Factories (powered by Eloquex-Factories) provide structured fake-data generation for model creation.
# database/seeders/database_seeder.ex
defmodule Database.Seeders.DatabaseSeeder do
def run do
# Create a known test user
user = Database.Factories.UserFactory.create(%{
"name" => "Test User",
"email" => "test@example.com"
})
{:ok, user}
end
endCall other seeders from DatabaseSeeder to keep concerns organized:
def run do
Database.Seeders.UserSeeder.run()
Database.Seeders.PostSeeder.run()
:ok
end./artisan db:seedThis invokes Database.Seeders.DatabaseSeeder.run/0. Seed after migrating:
./artisan migrate
./artisan db:seedOr reset and re-seed in one go:
./artisan migrate:fresh
./artisan db:seedFactories shine when you need many rows of realistic-looking data. See Eloquex-Factories for the full factory API; a quick example:
def run do
# Create 50 users
Database.Factories.UserFactory.count(50)
# Create 10 unverified users
Database.Factories.UserFactory.count(10, %{"email_verified_at" => nil})
:ok
endLaravex ships a Database.Factories.UserFactory that uses Laravex.Support.Faker for realistic-ish test data:
# database/factories/user_factory.ex
defmodule Database.Factories.UserFactory do
use Laravex.Eloquex.Factory, model: App.Models.User
def definition do
%{
"name" => fake_name(),
"email" => fake_email(),
"email_verified_at" => DateTime.utc_now() |> DateTime.to_iso8601(),
"password" => "password",
"remember_token" => Laravex.Support.Helpers.str_random(10)
}
end
defstate :unverified do
%{"email_verified_at" => nil}
end
endCall UserFactory.unverified/0 (or UserFactory.create_state(:unverified)) to create a user without email verification.
Laravex.Support.Faker provides lightweight deterministic helpers used inside factory definitions:
alias Laravex.Support.Faker
Faker.name() # => "Ada Baker"
Faker.first_name() # => "Lia"
Faker.last_name() # => "Patel"
Faker.email() # => "ada.baker4273@example.test"
Faker.word() # => "gentle"
Faker.sentence() # => "Alpha brave calm delta eager fresh gentle happy."
Faker.paragraph(3) # => "Alpha brave … Calm delta … Eager fresh …"
Faker.boolean() # => true | false
Faker.number(1, 100) # => 42
Faker.uuid() # => "550e8400-e29b-…"-
Eloquex-Factories — Full factory API:
count/2,sequence/0,defstate/2 - Migrations — Create the schema before seeding
- Testing — Using factories in test setups
Laravex — Laravel-faithful application skeleton & runtime for Elixir.
GitHub
·
Docs
·
Limitations
·
Laravel Docs
Not affiliated with Laravel LLC or the Elixir core team. Built to feel like Laravel on the BEAM.
- Home
- Installation
- Configuration
- Directory-Structure
- Request-Lifecycle
- Service-Container
- Service-Providers
- Deployment
- Routing
- Middleware
- CSRF-Protection
- Controllers
- Requests
- Responses
- Views
- Blade-Templates
- Asset-Bundling
- Session
- Validation
- Error-Handling
- Logging
- Artisan-Console
- Broadcasting
- Cache
- Collections
- Events
- File-Storage
- Helpers
- HTTP-Client
- Localization
- Notifications
- Pipeline
- Queues
- Rate-Limiting
- Strings
- Task-Scheduling