Run Laravel 5 seeder programmatically instead of from CLI
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Run Laravel Seeder Programmatically Instead of from CLI
As a senior developer, I frequently encounter situations where the standard development workflow—relying on the Artisan command line—is blocked by deployment constraints or specific hosting limitations. When you need to execute routine tasks like database seeding but cannot access the shell, you need an alternative method. The core question here is: Is there a way to run the Laravel seeder from within PHP rather than from the command line?
The short answer is yes. While Artisan commands are designed for CLI execution, we can bypass this mechanism and execute the underlying logic directly by interacting with the Laravel application's services and models. This approach is crucial when working in restricted environments, ensuring your application logic remains self-contained and runnable regardless of the execution context.
Why Bypass the CLI?
The command line interface (CLI) is excellent for rapid development and debugging, but it relies on an external process call (php artisan ...). When you need to embed this operation directly into your application flow—for instance, during a scheduled job, an API request, or a custom script executed by the host environment—relying on external processes can introduce unnecessary friction and potential security overhead.
By running the seeding logic directly within your PHP code, you ensure that the execution is tightly coupled with the application's runtime, making the operation more predictable and easier to manage in constrained environments. This philosophy aligns with building robust applications, much like the principles discussed on platforms like laravelcompany.com.
Method 1: Directly Interacting with Database Seeding
Instead of trying to replicate the entire db:seed command, which involves loading the Seeder class and running its run() method, a more efficient programmatic approach is often to directly manipulate the database using Eloquent models. This avoids the overhead of bootstrapping the full Artisan environment if you only need data population.
If your seeder logic primarily involves creating or updating records based on specific criteria, you can execute that logic within a standard service class or controller method.
Here is an example demonstrating how to manually populate data:
<?php
namespace App\Services;
use App\Models\User;
use Illuminate\Support\Facades\DB;
class ProgrammaticSeeder
{
public function run()
{
// 1. Create a user directly via Eloquent
$user = User::create([
'name' => 'Programmatic User',
'email' => 'program@example.com',
'password' => bcrypt('secret'),
]);
// 2. Insert multiple records using the DB facade for bulk operations
DB::table('products')->insert([
['name' => 'Widget A', 'price' => 10.50],
['name' => 'Gadget B', 'price' => 25.99],
]);
// Log success or handle errors here...
return true;
}
}
This method is highly effective because it directly leverages the ORM (Eloquent) and the database layer, which are fundamental components of any Laravel application. It allows you to achieve the result of seeding without needing the CLI wrapper.
Method 2: Calling Artisan Programmatically (The Bridge Approach)
If you absolutely need to execute an existing Seeder class (e.g., DatabaseSeeder) exactly as if you ran php artisan db:seed, you can use Laravel's internal reflection or the Artisan facade to trigger the command execution from within your application code.
<?php
namespace App\Jobs;
use Illuminate\Support\Facades\Artisan;
class RunSeederJob
{
public function handle()
{
// Execute the specific seed command programmatically
$result = Artisan::call('db:seed');
if ($result) {
// Successfully executed the command (check return value if applicable)
echo "Database seeding completed successfully via application code.\n";
} else {
// Handle potential errors returned by the CLI execution
throw new \Exception("Failed to run db:seed programmatically.");
}
}
}
While this method achieves the goal of running the command internally, it is generally less preferred for simple data population. It involves invoking an external process call within your PHP script, which can sometimes be slower and more complex to debug than direct Eloquent manipulation.
Conclusion
For most scenarios where you need to populate data or run specific seeding logic inside a restricted environment, Method 1 (Direct Database Interaction) is the superior choice. It keeps your application self-sufficient, avoids external process dependency, and provides cleaner, more predictable execution within the Laravel ecosystem. Use Method 2 only when you specifically need to invoke complex, pre-existing Artisan commands from within your code flow. Always strive to leverage the power of Eloquent and the framework services available on laravelcompany.com for optimal application architecture.