Thursday, February 15, 2018

Introduction create artisan command - Laravel

Step 1: Create custom artisan command 

php artisan make:command commandName

This will create new file called commandName.php with in folder commands which located in App\Console.

Step 2: Add our custom artisan command file in App\Console\kernel.php

<?php

namespace App\Console;

use App\Console\Commands\UserTableMigration;
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 = [
        commandName::class,
    ];

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

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

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

}

Step 3: Configure artisan command

App\Console\Commands\commandName.php

protected $signature = 'equo:user-migration';

Now we can run our custom migration by this.

php artisan equo:user-migration

protected $description = 'Migrate users from frontier db and fos db';


use this for add description for artisan command. (This will show when we run php artisan)

   public function handle()
    {
        $this->info($this->insertFrontierUserToDB());
    } 

This handle function is the place which are run when we execute artisan command.  with in this we can call models, Traits and all other functionalities.




Share/Bookmark

0 comments:

Post a Comment