{"id":19735,"date":"2021-08-05T10:27:26","date_gmt":"2021-08-05T10:27:26","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/blog\/?p=19735"},"modified":"2026-04-17T12:29:53","modified_gmt":"2026-04-17T12:29:53","slug":"task-scheduling-in-laravel-8","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/blog\/task-scheduling-in-laravel-8","title":{"rendered":"Task Scheduling in Laravel 8 Tutorial"},"content":{"rendered":"<p>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&#8217;s task details to their manager. <\/p>\n<h2>Task Scheduling in Laravel 8 Tutorial: What are we building?<\/h2>\n<p>Let\u2019s clarify what we are going to build in the demo. <\/p>\n<p>The user interface will have a form that will take details (employee with tasks) and the manager\u2019s email ID (to send a weekly report).<br \/>\nAfter submitting the form, the report will automatically be generated and mailed to the manager at the end of the week.<\/p>\n<h2>Create and Navigate to Laravel Project<\/h2>\n<p>Let\u2019s start a new Laravel project, for that run the below command in your cmd,<\/p>\n<pre>composer create-project laravel\/laravel task-scheduling-demo\r\ncd task-scheduling-demo<\/pre>\n<h2>Create Controller: TaskAddController<\/h2>\n<p>After creating the database and adding mail configurations to the .env file, use the following command to create a controller.<\/p>\n<pre>php artisan make:controller TaskAddController<\/pre>\n<h2>User Interface: Create Form<\/h2>\n<p>Moving towards the UI part. We need to create a form that will take employee and task details with the manager\u2019s email ID (the report will be sent).<\/p>\n<p>Open <strong>welcome.blade.php<\/strong> and add below code in your <strong><body><\/strong> tag.<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8.png\" alt=\"Task Scheduling in Laravel 8\" width=\"1850\" height=\"1000\" class=\"aligncenter size-full wp-image-21967\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8.png 1850w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8-300x162.png 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8-1024x554.png 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8-768x415.png 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8-1536x830.png 1536w\" sizes=\"auto, (max-width: 1850px) 100vw, 1850px\" \/><\/p>\n<p>The UI will look something like this-<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8-min.jpg\" alt=\"Task Scheduling in Laravel 8\" width=\"1100\" height=\"903\" class=\"aligncenter size-full wp-image-19744\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8-min-300x246.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8-min-1024x841.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8-min-768x630.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p class=\"boxed bg--secondary\" style=\"border: 1px solid #c7c7c7; box-shadow: 0 0 40px rgba(0, 0, 0, 0.2);\"><strong><em><span style=\"font-size:22px; color:#000;\">Are you in search of dedicated Laravel developers with optimum problem-solving skills for your project?<\/span><br \/>\nWe can be your one-stop solution. <a href=\"https:\/\/www.bacancytechnology.com\/hire-laravel-developer\" target=\"_blank\" rel=\"noopener\">Hire Laravel developer<\/a> from us to implement your visions into a successful reality<\/em><\/strong><\/p>\n<h2>Add Route<\/h2>\n<p>For adding route, use below code in <strong>web.php<\/strong> file<\/p>\n<pre>use App\\Http\\Controllers\\TaskAddController;\r\nRoute::post('\/addData',[TaskAddController::class,'addTask'])->name('addData');<\/pre>\n<h2>Create Model and Migration<\/h2>\n<p>In this step we will create one model and migration file to save the report of the employee. For that use the below command.<\/p>\n<pre>p h p artisan make:model AddTask -m<\/pre>\n<p>After creating the migration file, add table structure in it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8-.png\" alt=\"Task Scheduling in Laravel 8\" width=\"681\" height=\"964\" class=\"aligncenter size-full wp-image-21970\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8-.png 681w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8--212x300.png 212w\" sizes=\"auto, (max-width: 681px) 100vw, 681px\" \/><\/p>\n<h3>Update Controller<\/h3>\n<p>To update <strong>TaskAddController <\/strong>add below code:<\/p>\n<pre>< ?php\r\n \r\nnamespace App\\Http\\Controllers;\r\n \r\nuse Illuminate\\Http\\Request;\r\nuse App\\Models\\AddTask;\r\n \r\nclass TaskAddController extends Controller\r\n{\r\n   public function addTask(Request $request)\r\n   {\r\n       $data = $request->all();\r\n       $task = new AddTask();\r\n       $task- > e_id = $data['e_id'];\r\n       $task- > e_name = $data['e_name'];\r\n       $task- > e_email = $data['e_email'];\r\n       $task- > manager_email = $data['manager_email'];\r\n       $task- > t_mon = $data['t_mon'];\r\n       $task- > t_tue = $data['t_tue'];\r\n       $task- > t_wed = $data['t_wed'];\r\n       $task- > t_thu = $data['t_thu'];\r\n       $task- > t_fri = $data['t_fri'];\r\n       $task- > save();\r\n       return back()->with('status','Data added successfully');\r\n   }\r\n \r\n}\r\n<\/pre>\n<h3>Create Mail Class with Markdown<\/h3>\n<p>Now we <a href=\"https:\/\/www.bacancytechnology.com\/blog\/laravel-mail-example-using-markdown\" target=\"_blank\" rel=\"noopener\">create a mail class with a markdown<\/a>. For that apply the below command.<\/p>\n<pre>php artisan make:mail WeeklyReport --markdown=emails.weeklyReport<\/pre>\n<h3>Update Mail Class<\/h3>\n<p>After creating, update the mail class. <\/p>\n<p>Open <em>App\\Mail\\WeeklyReport.php<\/em> <\/p>\n<pre>< ?php\r\n \r\nnamespace App\\Mail;\r\n \r\nuse Illuminate\\Bus\\Queueable;\r\nuse Illuminate\\Contracts\\Queue\\ShouldQueue;\r\nuse Illuminate\\Mail\\Mailable;\r\nuse Illuminate\\Queue\\SerializesModels;\r\n \r\nclass WeeklyReport extends Mailable\r\n{\r\n    use Queueable, SerializesModels;\r\n \r\n    public $body;\r\n \r\n    \/**\r\n     * Create a new message instance.\r\n     *\r\n     * @return void\r\n     *\/\r\n    public function __construct($body)\r\n    {\r\n        $this->body = $body;\r\n    }\r\n \r\n    \/**\r\n     * Build the message.\r\n     *\r\n     * @return $this\r\n     *\/\r\n    public function build()\r\n    {\r\n        return $this->markdown('email.weeklyReport');\r\n    }\r\n}\r\n<\/pre>\n<p>For creating mail structure, open <strong>views\\email\\weeklyReport.blade.php<\/strong><\/p>\n<pre>Hello..\r\nThis mail contains Weekly report of your Team.\r\n<style>\r\n   table, th, td {\r\n        border: 1px solid black;\r\n   }\r\n   table {\r\n       width: 100%;\r\n       border-collapse: collapse;\r\n   }\r\n<\/style>\r\n<table >\r\n   <thead>\r\n   <tr>\r\n       <th>ID<\/th>\r\n       <th>Name<\/th>\r\n       <th>Email<\/th>\r\n       <th>Monday<\/th>\r\n       <th>Tuesday<\/th>\r\n       <th>Wednesday<\/th>\r\n       <th>Thursday<\/th>\r\n       <th>Friday<\/th>\r\n   <\/tr>\r\n   <\/thead>\r\n   <tbody>\r\n  \r\n   <tr>\r\n       <td>{{$body->e_id}}<\/td>\r\n       <td>{{$body->e_name}}<\/td>\r\n       <td>{{$body->e_email}}<\/td>\r\n       <td>{{$body->t_mon}}<\/td>\r\n       <td>{{$body->t_tue}}<\/td>\r\n       <td>{{$body->t_wed}}<\/td>\r\n       <td>{{$body->t_thu}}<\/td>\r\n       <td>{{$body->t_fri}}<\/td>\r\n   <\/tr>  \r\n   <\/tbody>\r\n<\/table><\/pre>\n<h2>Create Artisan command<\/h2>\n<p>For sending the weekly report we need to create an artisan command.<\/p>\n<p>Use the following command for the same<\/p>\n<pre>php artisan make:command sendWeeklyReport<\/pre>\n<p>Now go to the <mark>App\\Console\\Commands\\sendWeeklyReport.php<\/mark> and add the below code. <\/p>\n<p>The <strong>handle()<\/strong> 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\u2019s email ID.<\/p>\n<pre>< ?php\r\nnamespace App\\Console\\Commands;\r\nuse Illuminate\\Console\\Command;\r\nuse App\\Models\\AddTask;\r\nuse App\\Mail\\WeeklyReport;\r\nuse Mail;\r\nclass sendWeeklyReport extends Command\r\n{\r\n   \/**\r\n    * The name and signature of the console command.\r\n    *\r\n    * @var string\r\n    *\/\r\n   protected $signature = 'weekly:mail_report';\r\n   \/**\r\n    * The console command description.\r\n    *\r\n    * @var string\r\n    *\/\r\n   protected $description = 'Weekly report send to Manager';\r\n   \/**\r\n    * Create a new command instance.\r\n    *\r\n    * @return void\r\n    *\/\r\n   public function __construct()\r\n   {\r\n       parent::__construct();\r\n   }\r\n   \/**\r\n    * Execute the console command.\r\n    *\r\n    * @return int\r\n    *\/\r\n   public function handle()\r\n   {\r\n       $emp = AddTask::all();\r\n \r\n       foreach($emp as $empl)\r\n       {\r\n       $email = $empl->manager_email;\r\n       $body = $empl;\r\n       Mail::to($email)->send(new WeeklyReport($body));\r\n       $this->info('Weekly report has been sent successfully');\r\n       }\r\n   }\r\n}<\/pre>\n<h2>Schedule command<\/h2>\n<p>Open <strong>App\\Console\\Kernal.php<\/strong> and update the <em>schedule <\/em>method of that class to add a scheduler.<\/p>\n<pre>protected function schedule(Schedule $schedule)\r\n {\r\n    $schedule->command('weekly:mail_report')->weekly();\r\n }<\/pre>\n<p>To run the scheduler locally, use the following command<\/p>\n<pre>php artisan schedule:work<\/pre>\n<p>You can check the <a href=\"https:\/\/laravel.com\/docs\/7.x\/scheduling\" target=\"_blank\" rel=\"noopener\">Laravel official documentation<\/a> for exploring more about <em>Task scheduling in Laravel 8<\/em>.<\/p>\n<h2>Setup Crontab<\/h3>\n<p>So far, we have defined our scheduled tasks; now, let\u2019s 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\u2019s current time.<\/p>\n<p>To set up crontab, use the following instructions-<\/p>\n<ul class=\"bullets text-left\">\n<li>Open terminal and run <em>crontab -e<\/em>.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Scheduling-Demo-min.jpg\" alt=\"Laravel Task Scheduling Demo\" width=\"1100\" height=\"411\" class=\"aligncenter size-full wp-image-19745\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Scheduling-Demo-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Scheduling-Demo-min-300x112.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Scheduling-Demo-min-1024x383.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Scheduling-Demo-min-768x287.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<ul class=\"bullets text-left\">\n<li>Add the below line to the file.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Schedule-min.jpg\" alt=\"Laravel Task Schedule\" width=\"1100\" height=\"750\" class=\"aligncenter size-full wp-image-19746\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Schedule-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Schedule-min-300x205.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Schedule-min-1024x698.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Schedule-min-768x524.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<pre>* * * * * cd \/path-to-your-project && php artisan schedule:run >> \/dev\/null 2>&1<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Schedule-min.jpg\" alt=\"Laravel Task Schedule\" width=\"1100\" height=\"750\" class=\"aligncenter size-full wp-image-19746\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Schedule-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Schedule-min-300x205.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Schedule-min-1024x698.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Schedule-min-768x524.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<ul class=\"bullets text-left\">\n<li>Save the file.<\/li>\n<\/ul>\n<h3>Run the project<\/h3>\n<pre>php artisan serve<\/pre>\n<p>Fill the required details as shown below-<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8-min.jpg\" alt=\"Task Scheduling in Laravel 8\" width=\"1100\" height=\"903\" class=\"aligncenter size-full wp-image-19744\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8-min.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8-min-300x246.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8-min-1024x841.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Task-Scheduling-in-Laravel-8-min-768x630.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p>On submitting the form, the email will be sent.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Schedule-min-1.jpg\" alt=\"Laravel Task Schedule\" width=\"1100\" height=\"229\" class=\"aligncenter size-full wp-image-19748\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Schedule-min-1.jpg 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Schedule-min-1-300x62.jpg 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Schedule-min-1-1024x213.jpg 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2021\/08\/Laravel-Task-Schedule-min-1-768x160.jpg 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>So, this was about how to implement Task scheduling in Laravel 8. For more such tutorials, visit <a href=\"https:\/\/www.bacancytechnology.com\/tutorials\/laravel\" target=\"_blank\" rel=\"noopener\">Laravel Tutorials<\/a> Page and clone the github repository to play around with the code.<\/p>\n<p>If you have any queries or requirements for your Laravel project, feel free to connect with Bacancy to hire Laravel developers. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s task details to their manager. Task Scheduling in Laravel 8 Tutorial: What are we building? Let\u2019s clarify what we are going to build in the demo. [&hellip;]<\/p>\n","protected":false},"author":150,"featured_media":19750,"comment_status":"open","ping_status":"open","sticky":false,"template":"blog-new-template.php","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_lmt_disableupdate":"no","_lmt_disable":"","footnotes":""},"categories":[1142],"tags":[],"coauthors":[2325],"class_list":["post-19735","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel"],"acf":[],"modified_by":"Dhruvil Joshi","_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/19735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/users\/150"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=19735"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/19735\/revisions"}],"predecessor-version":[{"id":58584,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/19735\/revisions\/58584"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media\/19750"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=19735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=19735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=19735"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/coauthors?post=19735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}