PHP Lumen Call to a member function connection() on null

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Solving PHP Lumen Call to a member function connection() on null Issues with Eloquent Models Body:

Encountering the "Call to a member function connection() on null" error in your Lumen project while using an Eloquent Model can be confusing, especially if you're new to Lumen or API development. This problem may arise due to various reasons, some of which are easy to fix. In this blog post, we will explore the cause and solution for this issue and help you avoid further complications in your project.

Common Causes

  1. Not injecting database service in controller constructor: Ensure that you're properly using app('db') to access the database connection within your Lumen application. This will help prevent issues with null values.
  2. Using Eloquent models from different namespaces: If your model and controller are not placed in the same namespace, a null value may be returned when attempting to call functions on an Employee object, as it might think the connection is empty. Ensure that both files reside within the same namespace or update the autoloading configuration accordingly.
  3. Using Eloquent models without registering them: For using Eloquent classes, you must register them in your Lumen's service provider or bootstrap file (app/Providers/AppServiceProvider.php) to make sure the application can locate them when needed.

Solutions and Best Practices

  1. Inject database service in controller constructor: In your Lumen project, include app('db') within the construct method of all controllers that require access to the database connection, as shown below:
  2. public function __construct(Database $database) {
        $this->database = $database;
    }
    
  3. Ensure both model and controller belong in the same namespace: Make sure your Eloquent models and controllers are placed within the same namespace, or update your autoloading configuration to allow Lumen to locate them properly. Using namespaces prevents name conflicts that may result in null values.
  4. Register Eloquent models with Lumen: In your application's bootstrap file (app/Providers/AppServiceProvider.php), add the following code to register your Eloquent models:
  5. class AppServiceProvider extends ServiceProvider {
        protected $models = [
            Employee::class,
            // Other model classes
        ];
    
        public function boot() {
            foreach ($this->models as $model) {
                $this->app->register(\Illuminate\Foundation\Providers\ModelServiceProvider::class);
            }
        }
    }
    
  6. Use app('db')->select() for more complex queries: If you have to perform multiple database operations or need more advanced querying capabilities, use the app('db')->select() method. This will ensure that your Lumen application is using its database connection and avoiding issues with null values.

Conclusion

The "Call to a member function connection() on null" error in PHP Lumen can be caused by various factors, such as not injecting the database service or registering Eloquent models. By understanding the cause and implementing best practices to fix these issues, you can ensure the smooth running of your Lumen API project without any potential complications related to database connections.