How to Create Custom Artisan Command in Laravel

Artisan is a commonly used command-line interface that comes with Laravel and provides a set of commands that are useful when constructing your application.

Assume you want a command that will automatically deliver a link to the getting started guide to users one day after they register.

Let’s Start!

Create a command using following command

php artisan make:command SendRegisterLinkToUsers

OpenĀ app/Console/Commands/SendRegisterLinkToUsers .

Set name and signature of your command and write a small description about your command.

Inside handle function you can write your logic. It should look something like this:

<?php

namespace App\Console\Commands;

use App\Notifications\SendRegisterLinkNotification;
use App\User;
use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Send register link to user 1 day after registration.';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        User::whereNotNull('email_verified_at')
            ->whereDate('created_at', now()->subDays(1))
            ->get()->each(function ($user) {
                $user->notify(new SendRegisterLinkNotification());
            });
    }
}

Create Email Notification that we want to send to users.

php artisan make:notification SendRegisterLinkNotification

Make changes in app/Notifications/SendRegisterLinkNotification file as per your desire.

public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('Thanks for registering. Below is the link to our get started guide.')
                    ->action('View Get Started Guide', url('/getting_started_link'))
                    ->line('Feel free to reply to this email if you need any help in getting started.');
    }

Try running the command

php artisan users:send_register_link

To run it automatically every day, simply specify the command in the Laravel scheduler. This can be done in app/Console/Kernel.php.

protected function schedule(Schedule $schedule)
    {
        $schedule->command('users:send_register_link')->daily();
    }

Or You can directly use crontab to run the command everyday. No need to set Laravel Scheduler

To run command daily, you have to set Laravel Scheduler php artisan schedule:run as cron job that will run every minute

Here’s full command that you can directly set as cron job by opening crontab

php-path project-path php artisan schedule:run >> /dev/null$

Here’s php-path could be like depending on OS /usr/bin/php

That’s it

Hope you enjoy the article!

Leave a Reply: