DatabaseServiceProvider not booted on php artisan
Lumen 8.x
When I try
php artisan
I see error
Call to a member function connection() on null
Obviously database subsystem was not loaded.
If I add line
Model::setConnectionResolver($this->app['db']);
in "schedule" method in Kernel.php, everything works fine.
Why does Database not booted before running Kernel.php and how to fix this (I believe there is better solution than above one)?
@martinbean
Sure, here is the code
// app/Console/Kernel.php
<?php
namespace App\Console;
use App\Console\Commands\SomeCommand;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
Commands\SomeCommand::class,
];
/**
* Define the application's command schedule.
*
* @param Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule): void
{
// https://stackoverflow.com/questions/67565115/lumen-scheduler-call-to-a-member-function-connection-on-null
// temp fix, there must be better solution
Model::setConnectionResolver($this->app['db']);
$schedule
->command(SomeCommand::class)
->timezone(config('app.timezone'))
->dailyAt('09:00')
->withoutOverlapping()
->sendOutputTo('storage/logs/some-command.log', true);
}
}
The SomeCommand has DI SomeService which interacts with DB via SomeModel.
It does not work without the following line
Model::setConnectionResolver($this->app['db']);
Please or to participate in this conversation.