laravel artisan commands

How to create custom artisan commands in Laravel

Emmanuel Paul Mnzava.

--

Hello, Everyone

It’s been a while since i made my last post , was a bit stranded with projects but i am glad to have found the time to contribute some knowledge to you guys.

Today i will be going through how to create a custom artisan command in Laravel.

I am Doing so cause i have been receiving a lot of emails from you guys asking me to cover this topic, Well if it’s your first time let me assure you it’s so easy and you can also easily implement your own custom command by following the instructions below.

To easily scaffold a command we will be generating the necessary class by using another artisan command make:command ← This command will create a class in app/Console/Commands (directory) as seen in the example below.

Say i want to create a command that will send an email to all users in my Laravel application.I will first start by naming my command as SendNewsEmail then i will create my command as seen below

This will create a class on the path as seen below

app/Console/Commands/SendNewsEmails.php

Inside my command class i will have the following structure

i. Signature

ii. Description

iii. Constructor

iv. Handle Function

You can have a look on the code snippet below

<?php

namespace App\Console\Commands;

use App\Models\User;
use App\Support\DripEmailer;
use Illuminate\Console\Command;

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

/**
* The console command description.
*
* @var string
*/
protected $description = 'Send marketing email to all users';

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

/**
* Execute the console command.
*
* @param \App\Support\DripEmailer $drip
* @return mixed
*/
public function handle(DripEmailer $drip)
{
$users=User::all();
foreach($users as $user)
$drip->send($user);
}
}

Signature

  • Signature is the name of the console command you are free to name anything you want but for ease of use by you and other developers i suggest naming it something that is memorable. For example in our case we went with send:newsmail, remember to have two words separated by a colon.
  • To access this command one will run php artisan send:newsmail then just after that the handle function will be executed.

Description

As the name suggests this is the description of the command it tells other developers what the command is all about.

Handle

This is the function that handles all your command code execution or logic, In our case a loop that sends an email to all users.

Congratulations 🔥 🔥

You have made your first command for more information on how you can create advanced commands please visit this link

Before you go… I would like to introduce to you storewid.com, an eCommerce platform that can help you build, manage and grow your online store with ease.
Visit storewid.com and try it out for free.

Thanks for reading the article! If you enjoyed it, please don’t forget to show your appreciation by clicking 👏 below!

Any questions or comments hit me up on

Mail: epmnzava@gmail.com

Twitter: https://twitter.com/epmnzava

Github: https://github.com/dbrax

--

--

Emmanuel Paul Mnzava.

Software Engineer and techprenuer with passion of helping entreprenuers and small businesses using Technology.