Tinkerwell - The PHP Scratchpad

Streamlined Data Flow Between Laravel Seeders Using Context

Published on by

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

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Bacancy

Outsource a dedicated Laravel developer for $3,200/month. With over a decade of experience in Laravel development, we deliver fast, high-quality, and cost-effective solutions at affordable rates.

Visit Bacancy
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
"The Vibes" — NativePHP Hosts a Day 3 after Laracon US image

"The Vibes" — NativePHP Hosts a Day 3 after Laracon US

Read article
What We Know About Laravel 13 image

What We Know About Laravel 13

Read article
New Colors Added in Tailwind CSS v4.2 image

New Colors Added in Tailwind CSS v4.2

Read article
Factory makeMany() Method in Laravel 12.52.0 image

Factory makeMany() Method in Laravel 12.52.0

Read article
Laravel Adds an Official Svelte + Inertia Starter Kit image

Laravel Adds an Official Svelte + Inertia Starter Kit

Read article
MongoDB Vector Search in Laravel: Finding the Unqueryable image

MongoDB Vector Search in Laravel: Finding the Unqueryable

Read article