Laravel Tutorials

Streamlined Data Flow Between Laravel Seeders Using Context

Published
Streamlined Data Flow Between Laravel Seeders Using Context image

Database seeders frequently require shared data between classes, but conventional approaches create maintenance challenges and unclear dependencies. Laravel's Context system provides an elegant solution for managing seeder data flow.

Complex seeding scenarios often involve dependencies where subsequent seeders need access to previously created records. Traditional methods rely on static variables, global state, or redundant database queries, leading to fragile code and unclear relationships between seeder classes.

Laravel's Context facade transforms this challenge by providing a clean data sharing mechanism that maintains proper separation of concerns while enabling seamless information flow:

use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Context;

class UserSeeder extends Seeder
{
    public function run(): void
    {
        $adminUser = User::factory()->create([
            'email' => 'admin@company.com',
            'role' => 'administrator'
        ]);

        Context::add('admin_user', $adminUser);
        Context::add('admin_email', $adminUser->email);
    }
}

Dependent seeders can then access this shared data through type-hinted parameters using the Context attribute.

use App\Models\Project;
use App\Models\User;
use Illuminate\Container\Attributes\Context;
use Illuminate\Database\Seeder;

class ProjectSeeder extends Seeder
{
    public function run(
        #[Context('admin_user')] User $adminUser,
    ): void {
        $project = Project::factory()->create([
            'name' => 'Main Application',
            'owner_id' => $adminUser->id,
            'status' => 'active'
        ]);

        $project->members()->attach($adminUser, ['role' => 'project_manager']);
        
        Context::add('main_project', $project);
    }
}

Here's a comprehensive organizational structure seeding system demonstrating advanced Context usage:

use App\Models\{Organization, Department, User, Project, Role};
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Context;
use Illuminate\Container\Attributes\Context as ContextAttribute;

class OrganizationSeeder extends Seeder
{
    public function run(): void
    {
        $organization = Organization::factory()->create([
            'name' => 'Tech Solutions Inc',
            'type' => 'technology',
            'status' => 'active'
        ]);

        Context::add('organization', $organization);
        Context::add('org_id', $organization->id);
        
        $this->info("Created organization: {$organization->name}");
    }
}

class DepartmentSeeder extends Seeder
{
    public function run(
        #[ContextAttribute('organization')] Organization $organization
    ): void {
        $departments = collect([
            ['name' => 'Engineering', 'budget' => 500000],
            ['name' => 'Product Management', 'budget' => 200000],
            ['name' => 'Marketing', 'budget' => 300000],
            ['name' => 'Sales', 'budget' => 250000]
        ])->map(function ($dept) use ($organization) {
            return Department::factory()->create(array_merge($dept, [
                'organization_id' => $organization->id
            ]));
        });

        Context::add('departments', $departments);
        Context::add('engineering_dept', $departments->firstWhere('name', 'Engineering'));
        
        $this->info("Created {$departments->count()} departments");
    }
}

class UserSeeder extends Seeder
{
    public function run(
        #[ContextAttribute('organization')] Organization $organization,
        #[ContextAttribute('engineering_dept')] Department $engineeringDept
    ): void {
        $users = collect();
        
        $cto = User::factory()->create([
            'name' => 'Sarah Johnson',
            'email' => 'sarah.johnson@company.com',
            'role' => 'cto',
            'organization_id' => $organization->id,
            'department_id' => $engineeringDept->id
        ]);
        
        $users->push($cto);
        Context::add('cto', $cto);

        $teamLead = User::factory()->create([
            'name' => 'Mike Chen',
            'email' => 'mike.chen@company.com',
            'role' => 'team_lead',
            'organization_id' => $organization->id,
            'department_id' => $engineeringDept->id,
            'manager_id' => $cto->id
        ]);
        
        $users->push($teamLead);
        Context::add('team_lead', $teamLead);

        $developers = User::factory(5)->create([
            'role' => 'developer',
            'organization_id' => $organization->id,
            'department_id' => $engineeringDept->id,
            'manager_id' => $teamLead->id
        ]);
        
        $users = $users->concat($developers);
        Context::add('all_users', $users);
        Context::add('developers', $developers);
        
        $this->info("Created {$users->count()} users across the organization");
    }
}

class ProjectSeeder extends Seeder
{
    public function run(
        #[ContextAttribute('organization')] Organization $organization,
        #[ContextAttribute('cto')] User $cto,
        #[ContextAttribute('team_lead')] User $teamLead,
        #[ContextAttribute('developers')] \Illuminate\Support\Collection $developers
    ): void {
        $projects = collect();

        $mainProject = Project::factory()->create([
            'name' => 'Customer Portal Redesign',
            'description' => 'Complete overhaul of customer-facing portal',
            'status' => 'in_progress',
            'priority' => 'high',
            'organization_id' => $organization->id,
            'owner_id' => $cto->id,
            'lead_id' => $teamLead->id,
            'budget' => 150000,
            'deadline' => now()->addMonths(6)
        ]);

        $mainProject->members()->attach($cto, ['role' => 'project_owner']);
        $mainProject->members()->attach($teamLead, ['role' => 'project_lead']);
        
        $developers->each(function ($developer) use ($mainProject) {
            $mainProject->members()->attach($developer, ['role' => 'developer']);
        });

        $projects->push($mainProject);
        Context::add('main_project', $mainProject);

        $sideProject = Project::factory()->create([
            'name' => 'API Documentation System',
            'description' => 'Internal tool for API documentation',
            'status' => 'planning',
            'priority' => 'medium',
            'organization_id' => $organization->id,
            'owner_id' => $teamLead->id,
            'lead_id' => $developers->first()->id,
            'budget' => 50000,
            'deadline' => now()->addMonths(3)
        ]);

        $sideProject->members()->attach($teamLead, ['role' => 'project_owner']);
        $sideProject->members()->attach($developers->take(2), ['role' => 'developer']);

        $projects->push($sideProject);
        Context::add('all_projects', $projects);
        
        $this->info("Created {$projects->count()} projects with team assignments");
    }
}

class PermissionsSeeder extends Seeder
{
    public function run(
        #[ContextAttribute('cto')] User $cto,
        #[ContextAttribute('team_lead')] User $teamLead,
        #[ContextAttribute('developers')] \Illuminate\Support\Collection $developers
    ): void {
        $permissions = [
            'cto' => ['manage_organization', 'view_all_projects', 'manage_budgets', 'hire_staff'],
            'team_lead' => ['manage_team', 'view_team_projects', 'assign_tasks', 'review_code'],
            'developer' => ['view_assigned_projects', 'submit_code', 'create_tasks', 'update_profile']
        ];

        foreach ($permissions['cto'] as $permission) {
            $cto->permissions()->firstOrCreate(['name' => $permission]);
        }

        foreach ($permissions['team_lead'] as $permission) {
            $teamLead->permissions()->firstOrCreate(['name' => $permission]);
        }

        $developers->each(function ($developer) use ($permissions) {
            foreach ($permissions['developer'] as $permission) {
                $developer->permissions()->firstOrCreate(['name' => $permission]);
            }
        });

        $totalPermissions = array_sum(array_map('count', $permissions));
        $this->info("Assigned {$totalPermissions} permissions across all users");
    }
}

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        $this->call([
            OrganizationSeeder::class,
            DepartmentSeeder::class,
            UserSeeder::class,
            ProjectSeeder::class,
            PermissionsSeeder::class,
        ]);

        $this->info('Database seeding completed successfully');
    }
}

This approach eliminates anti-patterns like redundant database queries, static properties cluttering seeder classes, and tight coupling that makes testing difficult. The Context system provides type safety through attribute-based injection, ensuring proper autocompletion and maintainable code as seeding complexity grows.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux image

pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux

Read article
Laranon: Reversible PII Anonymization for Laravel and LLMs image

Laranon: Reversible PII Anonymization for Laravel and LLMs

Read article
PHP 8.6 is released!  image

PHP 8.6 is released!

Read article
Laravel Warrant: Row-Level Permissions With Eloquent image

Laravel Warrant: Row-Level Permissions With Eloquent

Read article
JSON Schema Validation Rules and a RetryFor Attribute in Laravel 13.43 image

JSON Schema Validation Rules and a RetryFor Attribute in Laravel 13.43

Read article
Durable Workflow: Long-Running Workflows for Laravel image

Durable Workflow: Long-Running Workflows for Laravel

Read article