Updating a table field with crop job the right way

Issue

This Content is from Stack Overflow. Question asked by Kairaoi T

I have followed some cron job examples trying to update one specific users table field named P using the following query. I want to set the value of P to 0 when timeout is null and logdate is less than the current time. The logdate is the logdate yesterday and Carbon::now() is the current date that is today. As for the testing I use everySecond(0.5) but the actual scenario is to run it everyday after working hours

UserPStatus Command

     public function handle()
    {

            DB::table('users')
            ->select('users.name', 'attendances.logdate', 'attendances.timeout')
            ->leftJoin('attendances','users.id','=','attendances.user_id')
            ->whereNull('attendances.timeout')
            ->where('attendances.logdate','<',Carbon::now())
            ->where('users.P',1)
            ->update([
                'P' => '0',              
            ]);
            
    }

Kernel

      class Kernel extends ConsoleKernel
        {

            // protected $commands = [
            //     CommandsUserPStatus::class,
            // ];
            /**
             * Define the application's command schedule.
             *
             * @param  IlluminateConsoleSchedulingSchedule  $schedule
             * @return void
             */
            protected function schedule(Schedule $schedule)
            {
                    $schedule->command('p:update')->everySeconds(0.5);
            }

            /**
             * Register the commands for the application.
             *
             * @return void
             */
            protected function commands()
            {
                $this->load(__DIR__.'/Commands');

                require base_path('routes/console.php');
            }
        }

With the two command below I did not see any updates to user.P value

  1. artisan schedule:run 1>> /dev/null 2>&1
  2. php artisan schedule:work.

Can anyone assist me



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?