Auto-Resolve Dependencies Anywhere in Laravel Using app()->call()

Execute methods with automatic dependency injection without manual wiring.

  • 28 Apr, 2026
  • 7 Views

Auto-Resolve Dependencies Anywhere in Laravel Using app()->call()

The Problem: Manual Dependency Injection

In Laravel, you often write:

$service = new ReportService(new PaymentGateway());
$service->generate();

Or in controllers:

public function store(Request $request, PaymentService $service)

👉 But what if you want to call a method dynamically?


🔥 The Solution: app()->call()

Laravel can automatically resolve dependencies for you.

app()->call(function (PaymentService $service) {
    return $service->process();
});

🔹 Real Project Example

Imagine running dynamic actions:

$callback = function (OrderService $service) {
    return $service->handle();
};
$result = app()->call($callback);

👉 Laravel injects OrderService automatically.


🔹 Call Class Method Dynamically

app()->call([ReportService::class, 'generate']);

No need to manually create instance.


🔹 Passing Extra Parameters

app()->call(function (UserService $service) {
    return $service->update(1);
});

Or:

app()->call([UserService::class, 'update'], ['id' => 1]);

🎯 Why This Is Powerful

It helps you:

  • Dynamically execute logic
  • Avoid manual dependency wiring
  • Leverage Laravel container fully
  • Build flexible systems

🧠 When to Use It

Use app()->call() when:

  • Executing dynamic callbacks
  • Building modular systems
  • Running configurable actions

Share: