How solve "Illuminate\Console\Command" not found?

99 Views Asked by At

I have a web application in a production environment. The path to the command is correctly set on the server, but it still throws error "Illuminate\Console\Command" not found. Solutions tryied -> (composer dump-autoload, deleted vendor and reinstalled with composer install, php artisan optimize). Nothing works :/

Fatal error: Uncaught Error: Class "Illuminate\Console\Command" not found in app/Console/Commands/DeleteExpiredData.php:10 Stack trace: #0 {main} thrown in /app/Console/Commands/DeleteExpiredData.php on line 10

Illuminatie is available in vendor and correctly installed, I did try composer dump-autoload but it still doesn't work.

This is my command

<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class DeleteExpiredData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'delete-expired-data';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Delete old data from database';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $now = Carbon::now();
        Log::info('Starting app:delete-expired-data command');

        DB::table('available_business_hour')
            ->whereDate('day', '<', $now->startOfDay())
            ->delete();

        Log::info('Expired command executed successfully');
    }

}

This is my kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     */
    protected function schedule(Schedule $schedule): void
    {
        $schedule->command('delete-expired-data')->daily();
        // $schedule->command('delete-unconfirmed-res')->everyFiveMinutes();
    }

    /**
     * Register the commands for the application.
     */
    protected function commands(): void
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }


And this is composer.json

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The skeleton application for the Laravel framework.",
    "keywords": ["laravel", "framework"],
    "license": "MIT",
    "require": {
        "php": "^8.1",
        "erlandmuchasaj/laravel-gzip": "^1.0",
        "guzzlehttp/guzzle": "^7.2",
        "jantinnerezo/livewire-alert": "^3.0",
        "laravel/framework": "^10.10",
        "laravel/sanctum": "^3.3",
        "laravel/tinker": "^2.8",
        "livewire/livewire": "^3.4",
        "propaganistas/laravel-phone": "^5.0"
    },
    "require-dev": {
        "fakerphp/faker": "^1.23",
        "laravel/pint": "^1.0",
        "laravel/sail": "^1.18",
        "mockery/mockery": "^1.4.4",
        "nunomaduro/collision": "^7.0",
        "phpunit/phpunit": "^10.1",
        "spatie/laravel-ignition": "^2.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi",
            "@php artisan vendor:publish --force --tag=livewire:assets --ansi"

        ],
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true,
        "allow-plugins": {
            "pestphp/pest-plugin": true,
            "php-http/discovery": true
        }
    },
    "minimum-stability": "stable",
    "prefer-stable": true
}

I'll be glad if anybody has idea how can I solve that issue :) Thank you

So I crated this custom script in www folder

#!/usr/bin/env php
<?php

define('LARAVEL_START', microtime(true));

require __DIR__.'/vendor/autoload.php';

$app = require_once __DIR__.'/bootstrap/app.php';

$commandName = 'app:delete-expired-data';

$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);

$input = new Symfony\Component\Console\Input\ArgvInput(['artisan', 'schedule:run']);

$status = $kernel->handle($input, new Symfony\Component\Console\Output\ConsoleOutput);

$kernel->terminate($input, $status);

exit($status);

In server cron log I have got this, is there any possible solution how to solve that? Because I can't edit proc_open on server.. :(

Tue 05 Mar 2024 04:35:23 PM CET Executing /srv/www/myDomain//barbershop27/scheduler.php

  2024-03-05 16:35:23 Running ['artisan' delete-expired-data] ....... 8ms DONE
  ⇂ '/usr/bin/php8.2' 'artisan' delete-expired-data > '/dev/null' 2>&1  

But in storage/logs it shows this error

production.ERROR: The Process class relies on proc_open, which is not available on your PHP installation. {"exception":"[object] (Symfony\Component\Process\Exception\LogicException(code: 0): The Process class relies on proc_open, which is not available on your PHP installation. at /srv/www/myDomain/barbershop27/vendor/symfony/process/Process.php:147)
[stacktrace]
#0 /srv/www/myDomain/barbershop27/vendor/symfony/process/Process.php(192): Symfony\Component\Process\Process->__construct(Array, '/srv/www/myDomain...', NULL, NULL, NULL)
1

There are 1 best solutions below

4
jwchild On

Okey,, finally a I did solve that with this option, and it works! :)

#!/usr/bin/env php
<?php

define('LARAVEL_START', microtime(true));

require __DIR__.'/vendor/autoload.php';

$app = require_once __DIR__.'/bootstrap/app.php';

$container = $app->make(Illuminate\Contracts\Container\Container::class);

$kernel = $container->make(Illuminate\Contracts\Console\Kernel::class);

$commandName = 'delete-expired-data';

$input = new Symfony\Component\Console\Input\ArgvInput(['console.php', $commandName]);

$status = $kernel->handle($input, new Symfony\Component\Console\Output\ConsoleOutput);

$kernel->terminate($input, $status);

exit($status);