PHP Lumen Call to a member function connection() on null
If you are migrating from full Laravel to Lumen for an API project, expecting Eloquent models to work identically out of the box is a common pitfall. The error Call to a member function connection() on null in this context indicates that the Eloquent model is attempting to access the database connection manager, but the underlying service container cannot resolve that connection. In other words, the database connection has not been registered or configured within your Lumen application instance.
While Lumen shares the core components of Laravel, including Eloquent and the Service Container, its default bootstrap process is significantly lighter. It does not automatically boot up the database facade or register the database connection service unless explicitly told to do so. When you instantiate an Employee model in your controller, Eloquent tries to access $this->getConnection(), which resolves to the database connection instance stored in the container. If that key is missing or null, the method call fails immediately.
To resolve this, you must ensure the database connection is registered before any models are instantiated. The most robust solution involves configuring your bootstrap/app.php file. You need to register the database package and optionally configure the connection details there. Here is a standard pattern for initializing the application in Lumen:
$app = new Illuminate\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
$app->singleton('Illuminate\Contracts\Http\Kernel',
\App\Http\Kernel::class
);
$app->singleton('Illuminate\Contracts\Console\Kernel',
\App\Console\Kernel::class
);
// Register the database package
$app->register('Illuminate\Database\EloquentServiceProvider');
// Alternatively, configure the connection directly if you are not using migrations yet
$app->configure('database');
However, simply registering the provider is often insufficient if you haven't defined the config/database.php file or if you are relying on a custom configuration. In many Lumen projects, developers create a config/database.php file to define the connections, migrations, and seeds arrays. If this file exists but isn't loaded, the container has no way to fetch the connection.
If you are using an older version of Lumen or have stripped down the configuration significantly, you might find that the database service is not available via the facade (DB). In such cases, you must ensure that the config/database.php file is present in your config/ directory and that it contains a valid connection definition, typically named mysql or pgsql.
Another frequent cause of this specific error in Lumen involves the use of custom database drivers or connections defined in the environment. If you are passing connection details via .env variables, ensure that the DB_CONNECTION, DB_HOST, and other relevant variables are set correctly before bootstrapping the app. The application loads these values early, but if the configuration file referencing them is missing, the driver fails to instantiate.
Regarding your question about executing queries directly using $app['db']->select(): this approach assumes that a service named db is registered in the container with that specific signature. In modern Laravel and Lumen versions, the standard facade is DB, which resolves to Illuminate\Database\DatabaseManager. The syntax you mentioned looks like a legacy pattern or a custom wrapper; it is generally safer to rely on the facades (DB::select) or Eloquent models once the connection is properly registered.
The key takeaway for Lumen development is that while the API-first philosophy encourages minimalism, it does not mean skipping essential service registrations. You must explicitly bring up the database layer if you intend to use Eloquent. Once the database configuration is loaded and the connection is defined in your config files, your standard Eloquent syntax like Employee::orderBy('first_name', 'asc')->get() will function exactly as it does in Laravel.