You can create a task (Console Command) in Laravel using Artisan. From your command line:
php artisan make:console MyTaskName
This creates the file in app/Console/Commands/MyTaskName.php. It will look like this:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class MyTaskName 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()
{
//
}
}
Some important parts of this definition are:
$signature
property is what identifies your command. You will be able to execute this command later through the command line using Artisan by running php artisan command:name
(Where command:name
matches your command's $signature
)$description
property is Artisan's help/usage displays next to your command when it is made available.handle()
method is where you write the code for your command.Eventually, your task will be made available to the command line through Artisan. The protected $signature = 'command:name';
property on this class is what you would use to run it.