Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

How to Scheduling Task using Cron Job in Laravel Application

Laravel Cronjob offers an elegant Task Scheduling or Laravel Cronjob Scheduling mechanism.

Applications require some tasks to be run periodically on the server. It could be creating backups or generating site traffic reports, sending promotional emails, or optimizing the database.

Step1- Open command prompt or Git Bash on xampp/htdocs directory

Step2- Create Laravel New Project write this command

composer create-project --prefer-dist laravel/laravel devopsschool "5.8.*"Code language: JavaScript (javascript)

Step3- Move to project directory on git bash

cd devopsschool

Step4- For User Authentication write this command

php artisan make:authCode language: CSS (css)

Step5- Create Database in Mysql Server

Step6- Set Mysql Server Username ,Password and Database Name in .env file

DB_DATABASE = devopsschool
DB_USERNAME = root
DB_PASSWORD =

Step7- Migration of Tables in Database

php artisan migrate

Step 8- Create the Laravel Artisan Command.
We use the make: console Artisan command to generate a command class skeleton to work with.

In this application, we will send one email to the owner telling us that we have these number of users registered today. So type the following command to generate our console command.

php artisan make:command RegisteredUsers --command=registered:users

The above command will create a class named RegisteredUsers in a file of the same name in the app/Console/Commands folder.

Step 9- We have also picked a name for the command via the command option. It is the name that we will use when calling the command. Now, open that command file RegisteredUsers.php.

protected $description = 'Send an email of registered users';Code language: PHP (php)

Step 10- We have just changed the description of the command. Now, we need to register this command inside the app >> Console >> Kernel.php file.

protected $commands = [
        'App\Console\Commands\RegisteredUsers',
    ];

Step 11 – We just now to call in via CronJob and get the job done. Now, write the handle method to get the number of users registered today.

public function handle()
{
$totalUsers = \DB::table('users')
->whereRaw('Date(created_at) = CURDATE()')
->count();
}

Step 12 – we need to send an email that contains that totalUsers. So let’s create a mail class

Create a mailable class to send the mail.
Type following command to generate mail class

php artisan make:mail SendMailableCode language: CSS (css)

So, it will create this file inside App\Mail\SendMailable.php.

Step 13 – Now, this class contains one property, and that is count. This count is the number of users that registered today. So SendMailable.php file looks like this.

<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendMailable extends Mailable
{
     use Queueable, SerializesModels;
     public $count;
     /** * Create a new message instance. * * @return void */
     public function __construct($count) {
       $this->count = $count;
    } 
    /** * Build the message. * * @return $this */
    public function build() {
     return $this->view('emails.registeredcount');
   }
}

Step 14- Also, define the view for this mail at resources >> views >> emails >> registeredcount.blade.php file. The mails folder is not there, so we need to create one and then add the view registeredcount.blade.php.

Total number of registered users for today is: {{ $count }}

Step 15- Now, add this mailable class inside the RegisteredUsers.php file.

Mail::to(Config::get('app.MAIL_FROM'))->send(new SendMailable($totalUsers));

Step 16- For sending a mail, I have used Mailtrap. You can quickly signup there. It is free for some usage. It fakes the email, so it is convenient to test our application.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your-username
MAIL_PASSWORD=your-password
MAIL_ENCRYPTION=null
MAIL_FROM=info@devopsschool.com
MAIL_FROM_NAME=DevopsSchool

Step 17- Configure MAIL_FROM and MAIL_FROM_NAME on config/app.php

'MAIL_FROM' => env('MAIL_FROM', 'not found in .env file'),
'MAIL_FROM_NAME' => env('MAIL_FROM_NAME', 'not found in .env file'),

Step 18- Now, type the following command to execute our code. Let us see that if we can get the mail.

php artisan registered:users

Step 19-Task Scheduler in Laravel
Task Scheduler in Laravel executes the artisan command, shell, or a callback periodically on the defined time. To do this, we use the schedule method in app/Console/Kernel.php, as we discussed earlier.

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

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

What is Laravel and use cases of Laravel?

What is Laravel? Laravel is an open-source, free PHP web framework designed for the development of web applications following the model-view-controller (MVC) architectural pattern. It was generated…

Read More

Complete guide of Laravel certification courses, tutorials & training

What is Laravel Laravel is a reliable and simple to use open-source PHP framework. It adheres to the model-view-controller pattern of design. Laravel makes use of pre-existing…

Read More

Top 100 laravel interview questions and answers

1) What is Laravel? Laravel is an open-source widely used PHP framework. The platform was intended for the development of web application by using MVC architectural pattern….

Read More

[SOLVED] Laravel : Supervisor FATAL/BACKOFF Exited too quickly (process log may have details)

Problem I’m trying to use Laravel queues with a supervisor but the service is not working properly. When I try to check status:$ sudo supervisorctl status$ laravel-worker:laravel-worker_00:…

Read More

How to Login with Token in Laravel PHP Framework?

How to Login with Token in Laravel PHP Framework? Step 1. Create a new Project, so write down the following command on Git Bash: Step 2. Move…

Read More

Directory Structure Of Laravel Application

devopsschool – This is Project Folder app – The app directory contains the core code of your application. bootstrap – The bootstrap directory contains the app.php file…

Read More