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
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell
Shift logo

Shift

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

Shift
Tinkerwell logo

Tinkerwell

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

Tinkerwell
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
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
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
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
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud

The latest

View all →
Monitor Laravel Queues, Commands, and Schedulers on Any Driver with Vigilance image

Monitor Laravel Queues, Commands, and Schedulers on Any Driver with Vigilance

Read article
Watch the Teaser for 'The Story of PHP' Documentary image

Watch the Teaser for 'The Story of PHP' Documentary

Read article
Ship AI with Laravel: Give Your AI Agent Live Web Search image

Ship AI with Laravel: Give Your AI Agent Live Web Search

Read article
Lattice: Describe Inertia UIs in PHP image

Lattice: Describe Inertia UIs in PHP

Read article
How We Cached Laravel News at the Edge with Fast Laravel image

How We Cached Laravel News at the Edge with Fast Laravel

Read article
The artisan dev Command in Laravel 13.16.0 image

The artisan dev Command in Laravel 13.16.0

Read article