This document provides a tutorial on implementing task scheduling in Laravel 8. It describes building a demo app that emails a weekly report of an employee's tasks to their manager. The tutorial covers creating a Laravel project, building a form to collect task details and the manager's email, saving data to a database, generating weekly reports with Markdown email templates, creating an Artisan command to send emails, and scheduling the command to run weekly.
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>
17. For adding route, use below code in
web.php file
use
AppHttpControllersTaskAddController;
Route::post('/addData',
[TaskAddController::class,'addTask'])-
>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');
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
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.