This document covers the database layer and data modeling architecture of the Laravel application. It explains the Eloquent ORM implementation, model factories for test data generation, and database seeding mechanisms. This page focuses on the core data structures and their relationships within the application.
For information about database configuration files and service integration, see Service Integration and External Dependencies. For testing infrastructure that utilizes these models, see Testing Infrastructure.
The application uses Laravel's Eloquent ORM as the primary database abstraction layer. Database configuration is managed through environment variables and configuration files, with different settings for development, testing, and production environments.
The testing environment uses an in-memory SQLite database for fast, isolated test execution. This configuration is defined in phpunit.xml25-26 which sets DB_CONNECTION=sqlite
and DB_DATABASE=:memory:
for all test runs.
Sources: phpunit.xml20-33 app/Models/User.php1-49 database/factories/UserFactory.php1-45
The application's primary model is the User
class, which extends Laravel's Authenticatable
base class and incorporates several traits for enhanced functionality.
The User
model demonstrates standard Eloquent patterns with mass assignment protection, attribute casting, and trait integration:
Component | Purpose | Implementation |
---|---|---|
Mass Assignment | Defines fillable attributes | $fillable array with name , email , password |
Attribute Hiding | Protects sensitive data in serialization | $hidden array with password , remember_token |
Type Casting | Automatic type conversion | casts() method defining email_verified_at as datetime, password as hashed |
Factory Integration | Test data generation | HasFactory trait linking to UserFactory |
Notifications | System notifications | Notifiable trait for email/SMS notifications |
Sources: app/Models/User.php10-48 database/factories/UserFactory.php12-44
The factory system provides a structured approach to generating test data using the UserFactory
class. This factory extends Laravel's base Factory
class and integrates with the Faker library for realistic data generation.
The UserFactory
implements several key methods for data generation:
definition()
method: Returns default attribute values for User model instancesunverified()
method: State modifier that creates users with unverified email addressesThe factory definition includes these attributes:
name
: Generated using fake()->name()
email
: Generated using fake()->unique()->safeEmail()
email_verified_at
: Set to current timestamp via now()
password
: Cached hashed version of "password"remember_token
: Random 10-character string via Str::random(10)
Sources: database/factories/UserFactory.php24-33 database/factories/UserFactory.php38-43
The seeding system populates the database with initial or test data using the DatabaseSeeder
class. This seeder demonstrates both factory usage and direct model creation patterns.
The DatabaseSeeder
class contains a run()
method that defines the database population strategy:
The seeder currently creates a single test user with predetermined attributes, overriding the factory's default name
and email
values. The commented line shows how to generate multiple users using the factory's default definition.
Sources: database/seeders/DatabaseSeeder.php14-22
The Composer configuration establishes PSR-4 autoloading for database-related classes, ensuring proper class resolution for models, factories, and seeders.
Namespace | Directory | Purpose |
---|---|---|
App\\ | app/ | Application models and core classes |
Database\\Factories\\ | database/factories/ | Model factories for test data |
Database\\Seeders\\ | database/seeders/ | Database seeding classes |
This organization supports Laravel's convention-based class discovery and enables automatic factory resolution through the HasFactory
trait.
Sources: composer.json22-27
The database and model system integrates closely with the testing infrastructure, providing isolated, fast test execution through in-memory SQLite databases and factory-generated test data.
The PHPUnit configuration establishes a complete testing environment with:
Sources: phpunit.xml20-33 tests/TestCase.php1-11 tests/Feature/ExampleTest.php1-20
Refresh this wiki