SlideShare a Scribd company logo
Task Scheduling
in Laravel 8
Tutorial
www.bacancytechnology.com
In this tutorial, we will learn how to
implement Task Scheduling in Laravel 8. We
will build a demo app that will e-mail the
weekly report of an employee’s task details
to their manager.
Task Scheduling in Laravel 8 Tutorial:
What are we building?
Create and Navigate to Laravel Project
Create Controller: TaskAddController
User Interface: Create Form
Add Route
Create Model and Migration
Update Mail Class
Create Artisan command
Schedule command
Conclusion
CONTENTS
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Task
Scheduling in
Laravel 8
Tutorial:
What are we
building?
Let’s clarify what we are going to build in
the demo.
The user interface will have a form that will
take details (employee with tasks) and the
manager’s email ID (to send a weekly
report).
After submitting the form, the report will
automatically be generated and mailed to
the manager at the end of the week.
Create and
Navigate to
Laravel
Project
Let’s start a new Laravel project, for that
run the below command in your cmd,
composer create-project laravel/laravel
task-scheduling-demo
cd task-scheduling-demo
Create
Controller:
TaskAddCont-
roller
After creating the database and adding mail
configurations to the .env file, use the
following command to create a controller.
php artisan make:controller
TaskAddController
User
Interface:
Create Form
Moving towards the UI part. We need to
create a form that will take employee and
task details with the manager’s email ID
(the report will be sent).
Open welcome.blade.php and add below
code in your tag.
<div class="container">
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
<form method="post" action="
{{route('addData')}}">
@csrf
<h2>Employee Details</h2><br>
<div class="form-group">
<label>Id</label>
<input type="number" name="e_id"
class="form-control" placeholder="Enter
Employee_Id">
</div>
<div class="form-group">
<label>Name</label>
<input type="text" name="e_name"
class="form-control" placeholder="Enter
Your Name">
</div>
<div class="form-group">
<label>Email address</label>
<input type="email" name="e_email"
class="form-control" placeholder="Enter
Your Email">
</div>
<div class="form-group">
<h2>Task Details</h2><br>
<div class="form-group">
<label>Monday</label>
<input type="text" name="t_mon"
class="form-control" placeholder="Task
done by Monday">
</div>
<div class="form-group">
<label>Tuesday</label>
<input type="text" name="t_tue"
class="form-control" placeholder="Task
done by Tuesday">
</div>
<div class="form-group">
<label>Wednesday</label>
<input type="text" name="t_wed"
class="form-control" placeholder="Task
done by Wednesday">
</div>
<div class="form-group">
<label>Thursday</label>
<input type="text" name="t_thu"
class="form-control" placeholder="Task
done by Thursday">
</div>
<div class="form-group">
<label>Friday</label>
<input type="text" name="t_fri"
class="form-control" placeholder="Task
done by Friday">
</div>
</div>
<div class="form-group"></div>
<button type="submit" class="btn btn-
primary">Submit</button>
</form>
</div>
The UI will look something like this-
Add Route
For adding route, use below code in
web.php file
use
AppHttpControllersTaskAddController;
Route::post('/addData',
[TaskAddController::class,'addTask'])-
&gt;name('addData');
Create Model
and Migration
In this step we will create one model and
migration file to save the report of the
employee. For that use the below command.
php artisan make:model AddTask -m
After creating the migration file, add table
structure in it.
<?php
use
IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
class CreateAddTasksTable extends
Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('add_tasks', function
(Blueprint $table) {
$table->id();
$table->bigInteger('e_id');
$table->string('e_name');
$table->string('e_email');
$table->string('manager_email');
$table->string('t_mon');
$table->string('t_tue');
$table->string('t_wed');
$table->string('t_thu');
$table->string('t_fri');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('add_tasks');
}
}
Update Controller
To update TaskAddController add below
code:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsAddTask;
class TaskAddController extends Controller
{
public function addTask(Request
$request)
{
$data = $request->all();
$task = new AddTask();
$task->e_id = $data['e_id'];
$task->e_name = $data['e_name'];
$task->e_email = $data['e_email'];
$task->manager_email =
$data['manager_email'];
$task->t_mon = $data['t_mon'];
$task->t_tue = $data['t_tue'];
$task->t_wed = $data['t_wed'];
$task->t_thu = $data['t_thu'];
$task->t_fri = $data['t_fri'];
$task->save();
return back()->with('status','Data added
successfully');
}
}
Create Mail
Class with
Markdown
Now we create a mail class with a
markdown. For that apply the below
command.
php artisan make:mail WeeklyReport --
markdown=emails.weeklyReport
Update Mail
Class
After creating, update the mail class.
Open AppMailWeeklyReport.php
<?php
namespace AppMail;
use IlluminateBusQueueable;
use
IlluminateContractsQueueShouldQueue;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;
class WeeklyReport extends Mailable
{
use Queueable, SerializesModels;
public $body;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($body)
{
$this->body = $body;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this-
>markdown('email.weeklyReport');
}
}
For creating mail structure, open
viewsemailweeklyReport.blade.php
Hello..
This mail contains Weekly report of your
Team.
<style>
table, th, td {
border: 1px solid black;
}
table {
width: 100%;
border-collapse: collapse;
}
</style>
<table >
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{$body->e_id}}</td>
<td>{{$body->e_name}}</td>
<td>{{$body->e_email}}</td>
<td>{{$body->t_mon}}</td>
<td>{{$body->t_tue}}</td>
<td>{{$body->t_wed}}</td>
<td>{{$body->t_thu}}</td>
<td>{{$body->t_fri}}</td>
</tr>
</tbody>
</table>
Create
Artisan
command
For sending the weekly report we need to
create an artisan command .
Use the following the command for the
same
php artisan make:command
sendWeeklyReport
Now go to the
AppConsoleCommandssendWeeklyRepo
rt.php and add the below code.
The handle() method of this class gets all the
data from the database; for each employee
report will be generated and sent to the
email ID provided in the form, here
manager’s email ID.
<?php
namespace AppConsoleCommands;
use IlluminateConsoleCommand;
use AppModelsAddTask;
use AppMailWeeklyReport;
use Mail;
class sendWeeklyReport extends Command
{
/**
* The name and signature of the console
command.
*
* @var string
*/
protected $signature =
'weekly:mail_report';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Weekly report
send to Manager';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$emp = AddTask::all();
foreach($emp as $empl)
{
$email = $empl->manager_email;
$body = $empl;
Mail::to($email)->send(new
WeeklyReport($body));
$this->info('Weekly report has been sent
successfully');
}
}
}
Schedule
command
Open AppConsoleKernal.php and update
the schedule method of that class to add a
scheduler.
protected function schedule(Schedule
$schedule)
{
$schedule-
>command('weekly:mail_report')-
>weekly();
}
To run the scheduler locally, use the
following command
php artisan schedule:work
You can check the Laravel official
documentation for exploring more about
Task scheduling in Laravel 8.
Open terminal and run crontab -e.
So far, we have defined our scheduled tasks;
now, let’s see how we can run them on the
server. The artisan command schedule:run
evaluates all the scheduled tasks and
determines whether they need to be run on
the server’s current time.
To set up crontab, use the following
instructions-
Setup Crontab
Add the below line to the file.
Save the file.
* * * * * cd /path-to-your-project && php
artisan schedule:run >> /dev/null 2>&1
Run the project
php artisan serve
Fill the required details as shown below-
On submitting the form, the email will be
sent.
Conclusion
So, this was about how to implement Task
scheduling in Laravel 8. For more such
tutorials, visit Laravel Tutorials Page and
clone the github repository to play around
with the code.
If you have any queries or requirements for
your Laravel project, feel free to connect
with Bacancy to hire Laravel developers.
Thank You
www.bacancytechnology.com

More Related Content

What's hot (20)

PDF
Installing php 7.4 Nginx Laravel 7.x on Centos 8
Raja Rozali Raja Hasan
 
PDF
Adventures in Laravel 5 SunshinePHP 2016 Tutorial
Joe Ferguson
 
PPTX
Introduction to Laravel Framework (5.2)
Viral Solani
 
PPTX
Laravel for Web Artisans
Raf Kewl
 
PDF
RESTful API development in Laravel 4 - Christopher Pecoraro
Christopher Pecoraro
 
PDF
Getting to know Laravel 5
Bukhori Aqid
 
PPTX
Laravel Beginners Tutorial 1
Vikas Chauhan
 
PDF
Laravel presentation
Toufiq Mahmud
 
PPTX
Introduction to laravel framework
Ahmad Fatoni
 
PDF
Why Laravel?
Jonathan Goode
 
PPTX
Laravel5 Introduction and essentials
Pramod Kadam
 
PPT
Laravel Starter Kit | Laravel Admin Template-ChandraAdmin
Lorvent56
 
PDF
What's New In Laravel 5
Darren Craig
 
PPTX
Laravel 5
Sudip Simkhada
 
PDF
Laravel 5 Annotations: RESTful API routing
Christopher Pecoraro
 
PPT
Web service with Laravel
Abuzer Firdousi
 
PDF
Intro to Laravel 4
Singapore PHP User Group
 
PPTX
10 Laravel packages everyone should know
Povilas Korop
 
ODP
Javascript laravel's friend
Bart Van Den Brande
 
PDF
Laravel Restful API and AngularJS
Blake Newman
 
Installing php 7.4 Nginx Laravel 7.x on Centos 8
Raja Rozali Raja Hasan
 
Adventures in Laravel 5 SunshinePHP 2016 Tutorial
Joe Ferguson
 
Introduction to Laravel Framework (5.2)
Viral Solani
 
Laravel for Web Artisans
Raf Kewl
 
RESTful API development in Laravel 4 - Christopher Pecoraro
Christopher Pecoraro
 
Getting to know Laravel 5
Bukhori Aqid
 
Laravel Beginners Tutorial 1
Vikas Chauhan
 
Laravel presentation
Toufiq Mahmud
 
Introduction to laravel framework
Ahmad Fatoni
 
Why Laravel?
Jonathan Goode
 
Laravel5 Introduction and essentials
Pramod Kadam
 
Laravel Starter Kit | Laravel Admin Template-ChandraAdmin
Lorvent56
 
What's New In Laravel 5
Darren Craig
 
Laravel 5
Sudip Simkhada
 
Laravel 5 Annotations: RESTful API routing
Christopher Pecoraro
 
Web service with Laravel
Abuzer Firdousi
 
Intro to Laravel 4
Singapore PHP User Group
 
10 Laravel packages everyone should know
Povilas Korop
 
Javascript laravel's friend
Bart Van Den Brande
 
Laravel Restful API and AngularJS
Blake Newman
 

Similar to Task scheduling in laravel 8 tutorial (20)

PDF
Aspnet mvc tutorial_01_cs
Alfa Gama Omega
 
PDF
Laravel 8 export data as excel file with example
Katy Slemon
 
PPT
Test
guest25229c
 
PPTX
Devise and Rails
William Leeper
 
PPT
Asp.net tips
actacademy
 
DOCX
LearningMVCWithLINQToSQL
Akhil Mittal
 
PDF
ASP.NET Identity
Suzanne Simmons
 
PPTX
Ways to Set Focus on an Input Field After Rendering in React.pptx
BOSC Tech Labs
 
PPTX
Reactive application using meteor
Sapna Upreti
 
PDF
How to Build ToDo App with Vue 3 + TypeScript
Katy Slemon
 
PDF
METEOR 101
Tin Aung Lin
 
PDF
A To-do Web App on Google App Engine
Michael Parker
 
PPTX
Implementation of Push Notification in React Native Android app using Firebas...
naseeb20
 
PDF
Jsf intro
vantinhkhuc
 
ODP
Well
breccan
 
PPTX
Angularjs2 presentation
dharisk
 
PPTX
ReactJS.pptx
SamyakShetty2
 
ODP
Mvc - Titanium
Prawesh Shrestha
 
PDF
To-Do App With Flutter: Step By Step Guide
Biztech Consulting & Solutions
 
PDF
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Nicolás Bouhid
 
Aspnet mvc tutorial_01_cs
Alfa Gama Omega
 
Laravel 8 export data as excel file with example
Katy Slemon
 
Devise and Rails
William Leeper
 
Asp.net tips
actacademy
 
LearningMVCWithLINQToSQL
Akhil Mittal
 
ASP.NET Identity
Suzanne Simmons
 
Ways to Set Focus on an Input Field After Rendering in React.pptx
BOSC Tech Labs
 
Reactive application using meteor
Sapna Upreti
 
How to Build ToDo App with Vue 3 + TypeScript
Katy Slemon
 
METEOR 101
Tin Aung Lin
 
A To-do Web App on Google App Engine
Michael Parker
 
Implementation of Push Notification in React Native Android app using Firebas...
naseeb20
 
Jsf intro
vantinhkhuc
 
Well
breccan
 
Angularjs2 presentation
dharisk
 
ReactJS.pptx
SamyakShetty2
 
Mvc - Titanium
Prawesh Shrestha
 
To-Do App With Flutter: Step By Step Guide
Biztech Consulting & Solutions
 
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Nicolás Bouhid
 
Ad

More from Katy Slemon (20)

PDF
React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
Katy Slemon
 
PDF
Data Science Use Cases in Retail & Healthcare Industries.pdf
Katy Slemon
 
PDF
How Much Does It Cost To Hire Golang Developer.pdf
Katy Slemon
 
PDF
What’s New in Flutter 3.pdf
Katy Slemon
 
PDF
Why Use Ruby On Rails.pdf
Katy Slemon
 
PDF
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
Katy Slemon
 
PDF
How to Implement Middleware Pipeline in VueJS.pdf
Katy Slemon
 
PDF
How to Build Laravel Package Using Composer.pdf
Katy Slemon
 
PDF
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
Katy Slemon
 
PDF
How to Develop Slack Bot Using Golang.pdf
Katy Slemon
 
PDF
IoT Based Battery Management System in Electric Vehicles.pdf
Katy Slemon
 
PDF
Understanding Flexbox Layout in React Native.pdf
Katy Slemon
 
PDF
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
Katy Slemon
 
PDF
New Features in iOS 15 and Swift 5.5.pdf
Katy Slemon
 
PDF
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
Katy Slemon
 
PDF
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
Katy Slemon
 
PDF
Flutter Performance Tuning Best Practices From the Pros.pdf
Katy Slemon
 
PDF
Angular Universal How to Build Angular SEO Friendly App.pdf
Katy Slemon
 
PDF
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
Katy Slemon
 
PDF
Ruby On Rails Performance Tuning Guide.pdf
Katy Slemon
 
React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
Katy Slemon
 
Data Science Use Cases in Retail & Healthcare Industries.pdf
Katy Slemon
 
How Much Does It Cost To Hire Golang Developer.pdf
Katy Slemon
 
What’s New in Flutter 3.pdf
Katy Slemon
 
Why Use Ruby On Rails.pdf
Katy Slemon
 
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
Katy Slemon
 
How to Implement Middleware Pipeline in VueJS.pdf
Katy Slemon
 
How to Build Laravel Package Using Composer.pdf
Katy Slemon
 
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
Katy Slemon
 
How to Develop Slack Bot Using Golang.pdf
Katy Slemon
 
IoT Based Battery Management System in Electric Vehicles.pdf
Katy Slemon
 
Understanding Flexbox Layout in React Native.pdf
Katy Slemon
 
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
Katy Slemon
 
New Features in iOS 15 and Swift 5.5.pdf
Katy Slemon
 
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
Katy Slemon
 
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
Katy Slemon
 
Flutter Performance Tuning Best Practices From the Pros.pdf
Katy Slemon
 
Angular Universal How to Build Angular SEO Friendly App.pdf
Katy Slemon
 
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
Katy Slemon
 
Ruby On Rails Performance Tuning Guide.pdf
Katy Slemon
 
Ad

Recently uploaded (20)

PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 

Task scheduling in laravel 8 tutorial

  • 1. Task Scheduling in Laravel 8 Tutorial www.bacancytechnology.com
  • 2. In this tutorial, we will learn how to implement Task Scheduling in Laravel 8. We will build a demo app that will e-mail the weekly report of an employee’s task details to their manager.
  • 3. Task Scheduling in Laravel 8 Tutorial: What are we building? Create and Navigate to Laravel Project Create Controller: TaskAddController User Interface: Create Form Add Route Create Model and Migration Update Mail Class Create Artisan command Schedule command Conclusion CONTENTS 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
  • 5. Let’s clarify what we are going to build in the demo. The user interface will have a form that will take details (employee with tasks) and the manager’s email ID (to send a weekly report). After submitting the form, the report will automatically be generated and mailed to the manager at the end of the week.
  • 7. Let’s start a new Laravel project, for that run the below command in your cmd, composer create-project laravel/laravel task-scheduling-demo cd task-scheduling-demo
  • 9. After creating the database and adding mail configurations to the .env file, use the following command to create a controller. php artisan make:controller TaskAddController
  • 11. Moving towards the UI part. We need to create a form that will take employee and task details with the manager’s email ID (the report will be sent). Open welcome.blade.php and add below code in your tag. <div class="container"> @if (session('status')) <div class="alert alert-success"> {{ session('status') }} </div> @endif <form method="post" action=" {{route('addData')}}"> @csrf <h2>Employee Details</h2><br> <div class="form-group"> <label>Id</label>
  • 12. <input type="number" name="e_id" class="form-control" placeholder="Enter Employee_Id"> </div> <div class="form-group"> <label>Name</label> <input type="text" name="e_name" class="form-control" placeholder="Enter Your Name"> </div> <div class="form-group"> <label>Email address</label> <input type="email" name="e_email" class="form-control" placeholder="Enter Your Email"> </div>
  • 13. <div class="form-group"> <h2>Task Details</h2><br> <div class="form-group"> <label>Monday</label> <input type="text" name="t_mon" class="form-control" placeholder="Task done by Monday"> </div> <div class="form-group"> <label>Tuesday</label> <input type="text" name="t_tue" class="form-control" placeholder="Task done by Tuesday"> </div> <div class="form-group"> <label>Wednesday</label> <input type="text" name="t_wed" class="form-control" placeholder="Task done by Wednesday"> </div>
  • 14. <div class="form-group"> <label>Thursday</label> <input type="text" name="t_thu" class="form-control" placeholder="Task done by Thursday"> </div> <div class="form-group"> <label>Friday</label> <input type="text" name="t_fri" class="form-control" placeholder="Task done by Friday"> </div> </div> <div class="form-group"></div> <button type="submit" class="btn btn- primary">Submit</button> </form> </div>
  • 15. The UI will look something like this-
  • 17. For adding route, use below code in web.php file use AppHttpControllersTaskAddController; Route::post('/addData', [TaskAddController::class,'addTask'])- &gt;name('addData');
  • 19. In this step we will create one model and migration file to save the report of the employee. For that use the below command. php artisan make:model AddTask -m After creating the migration file, add table structure in it. <?php use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema;
  • 20. class CreateAddTasksTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('add_tasks', function (Blueprint $table) { $table->id(); $table->bigInteger('e_id'); $table->string('e_name'); $table->string('e_email'); $table->string('manager_email'); $table->string('t_mon');
  • 21. $table->string('t_tue'); $table->string('t_wed'); $table->string('t_thu'); $table->string('t_fri'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('add_tasks'); } }
  • 22. Update Controller To update TaskAddController add below code: <?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsAddTask; class TaskAddController extends Controller { public function addTask(Request $request) { $data = $request->all(); $task = new AddTask(); $task->e_id = $data['e_id'];
  • 23. $task->e_name = $data['e_name']; $task->e_email = $data['e_email']; $task->manager_email = $data['manager_email']; $task->t_mon = $data['t_mon']; $task->t_tue = $data['t_tue']; $task->t_wed = $data['t_wed']; $task->t_thu = $data['t_thu']; $task->t_fri = $data['t_fri']; $task->save(); return back()->with('status','Data added successfully'); } }
  • 25. Now we create a mail class with a markdown. For that apply the below command. php artisan make:mail WeeklyReport -- markdown=emails.weeklyReport
  • 27. After creating, update the mail class. Open AppMailWeeklyReport.php <?php namespace AppMail; use IlluminateBusQueueable; use IlluminateContractsQueueShouldQueue; use IlluminateMailMailable; use IlluminateQueueSerializesModels; class WeeklyReport extends Mailable { use Queueable, SerializesModels; public $body; /** * Create a new message instance.
  • 28. * * @return void */ public function __construct($body) { $this->body = $body; } /** * Build the message. * * @return $this */ public function build() { return $this- >markdown('email.weeklyReport'); } }
  • 29. For creating mail structure, open viewsemailweeklyReport.blade.php Hello.. This mail contains Weekly report of your Team. <style> table, th, td { border: 1px solid black; } table { width: 100%; border-collapse: collapse; } </style> <table > <thead> <tr>
  • 33. For sending the weekly report we need to create an artisan command . Use the following the command for the same php artisan make:command sendWeeklyReport Now go to the AppConsoleCommandssendWeeklyRepo rt.php and add the below code. The handle() method of this class gets all the data from the database; for each employee report will be generated and sent to the email ID provided in the form, here manager’s email ID.
  • 34. <?php namespace AppConsoleCommands; use IlluminateConsoleCommand; use AppModelsAddTask; use AppMailWeeklyReport; use Mail; class sendWeeklyReport extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'weekly:mail_report'; /** * The console command description. * * @var string */
  • 35. protected $description = 'Weekly report send to Manager'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return int */ public function handle() { $emp = AddTask::all();
  • 36. foreach($emp as $empl) { $email = $empl->manager_email; $body = $empl; Mail::to($email)->send(new WeeklyReport($body)); $this->info('Weekly report has been sent successfully'); } } }
  • 38. Open AppConsoleKernal.php and update the schedule method of that class to add a scheduler. protected function schedule(Schedule $schedule) { $schedule- >command('weekly:mail_report')- >weekly(); } To run the scheduler locally, use the following command php artisan schedule:work You can check the Laravel official documentation for exploring more about Task scheduling in Laravel 8.
  • 39. Open terminal and run crontab -e. So far, we have defined our scheduled tasks; now, let’s see how we can run them on the server. The artisan command schedule:run evaluates all the scheduled tasks and determines whether they need to be run on the server’s current time. To set up crontab, use the following instructions- Setup Crontab
  • 40. Add the below line to the file. Save the file. * * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
  • 41. Run the project php artisan serve Fill the required details as shown below-
  • 42. On submitting the form, the email will be sent.
  • 44. So, this was about how to implement Task scheduling in Laravel 8. For more such tutorials, visit Laravel Tutorials Page and clone the github repository to play around with the code. If you have any queries or requirements for your Laravel project, feel free to connect with Bacancy to hire Laravel developers.