user7432810
user7432810

Reputation: 695

Calling laravel artisan command from console

I am trying to teach myself Laravel command so that later I use it for scheduling them. This is my kernel file:

    namespace App\Console;

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

    class Kernel extends ConsoleKernel
    {
        /**
         * The Artisan commands provided by your application.
         *
         * @var array
         */
        protected $commands = [
            //
            'App\Console\Commands\FooCommand',      
        ];

        /**
         * Define the application's command schedule.
         *
         * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
         * @return void
         */
        protected function schedule(Schedule $schedule)
        {
            // $schedule->command('inspire')
            //          ->hourly();
            $schedule->command('App\Console\Commands\FooCommand')->hourly();
        }

        /**
         * Register the Closure based commands for the application.
         *
         * @return void
         */
        protected function commands()
        {
            require base_path('routes/console.php');
        }
    }

And this is the command file inside \App\Console\Commands

namespace App\Console\Commands;

use Illuminate\Console\Command;

class FooCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }

    public function fire()
    {

        $this->info('Test has fired.');
    }   
}

I want to test the FooCommand command. How do it call this command from shell so that the result is "Test has fired."?

Upvotes: 8

Views: 55385

Answers (1)

marcosrjjunior
marcosrjjunior

Reputation: 281

to run your commands manually: php artisan command:name.

Remove your fire function, you can handle this inside handle function.

Fix your schedule function at Kernel class

class Kernel extends ConsoleKernel
{
    ....

    protected function schedule(Schedule $schedule)
    {
        $schedule->command('command:name')
            ->hourly();
    }
}

To config your schedule please read this: https://laravel.com/docs/5.4/scheduling

Upvotes: 16

Related Questions