PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 18, 2022

[FIXED] why my cron job is not working in laravel?

 January 18, 2022     command, console, cron, laravel-5, php     No comments   

Issue

in db i have column visit_clear i want it 0 after one day so i used this code in kernal.php

   <?php
    
    namespace App\Console;
    
    use Illuminate\Console\Scheduling\Schedule;
    use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
    
    class Kernel extends ConsoleKernel
    {
        protected $commands = [
        ];
        protected function schedule(Schedule $schedule)
        {
            $schedule->command('cron:update-user-not-new')->daily();
        }
    
        protected function commands()
        {
            $this->load(__DIR__.'/Commands');
    
            require base_path('routes/console.php');
        }
    }

and in command/UpdateUserNotNew.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use Illuminate\Support\Facades\DB;

class UpdateUserNotNew extends Command
{
 
    protected $signature = 'cron:update-user-not-new';

    protected $description = 'Command description';



    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $dayAgo = 1; // Days ago
        $dayToCheck = \Carbon\Carbon::now()->subDays($dayAgo)->format('Y-m-d');
        Customer::whereDate('visit_date', '<=', $dayToCheck)
        ->update([
            'visit_clear' => 0
        ]);
    }
}

i am sheduling commnd like this as u can see cron:update-user-not-new should i use crone:UpdateUserNotNew?


Solution

You need to register your command in Kernel.php like this:

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

You should then be able to run the command manually with php artisan cron:update-user-not-new

In order for the automatic running of the command to work, you need to add an entry to your system's task scheduler, as this is what Laravel uses to run commands on a schedule.

Assuming you are using Linux, you need to add an entry to your crontab. To do this, in a command prompt enter crontab -e, hit enter, and add this line:

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

Don't forget to replace /path-to-your-project with the root folder of your project Once done editing the crontab, save and close the editor and the new entries should be installed, and your command should now run on the schedule.

All this info came from https://laravel.com/docs/7.x/scheduling so if you need more info take a look there



Answered By - Matt
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing