Create Custom Artisan Command in Laravel
Tags: artisan, command, laravel
Published: 2020-06-14 04:58:06

---
title: Create Custom Artisan Command in Laravel
published: true
description: Create Custom Artisan Command in Laravel to automatically email getting started guide link to users 1 day after registration.
tags: artisan, command, laravel
cover_image: https://images.unsplash.com/photo-1518607692857-bff9babd9d40?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=667&q=80
---

Artisan is a frequently used command-line interface that comes with Laravel which provides a set of commands that are helpful while building your application.

Let’s say you want a command to automatically email getting started guide link to users 1 day after registration.

If you would like to watch me doing this then here is the tutorial.
{% youtube Ik927SLPTi0 %}

Let's Start!

Create a new command using command:
```bash
php artisan make:command SendDocLinkToUsers
```

Open **app/Console/Commands/SendDocLinkToUsers**.

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
<?php

namespace App\Console\Commands;

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

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Send doc link to users 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 SendDocLinkNotification());
            });
    }
}

```

Create Email Notification that we want to send to users.
```bash
php artisan make:notification SendDocLinkNotification
```

Make changes in app/Notifications/SendDocLinkNotification file as per your liking.
```php
    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_guide_url'))
                    ->line('Feel free to reply to this email if you need any help in getting started.');
    }
```

Try running the command
```bash
php artisan users:send_doc_link
```


Now to run it auto automatically everyday, all you need to do is set the command in Laravel scheduler. You can do this in **app/Console/Kernel.php**

```php
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('users:send_doc_link')->daily()->at('08:00');
    }
```


Or You can even use crontab to run the command everyday.
![Running command using crontab](https://dev-to-uploads.s3.amazonaws.com/i/4plg7m384iynv213v16r.png)

*Note: For running Laravel scheduler you will have to set the command in either crontab or supervisor to run every minute.

![Crontab entry for running Laravel Scheduler](https://dev-to-uploads.s3.amazonaws.com/i/7c8zmavjjhja6b0ezzpd.png)

You can learn more about Laravel scheduler here: https://laravel.com/docs/7.x/scheduling#introduction

You can find the entire code [here](https://github.com/csinghdev/laravel-examples)

That's it!

Hope you find it useful.