Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

demonz's avatar

how do i update column status on condition automatically

Hello I want to update status column for certain data , based if the start date of the data is passed ? It’s better to let it check daily at 12am instead of every minute

0 likes
8 replies
webrobert's avatar

Depends on what it is?

for example I have a project management app. The start date is a day not a time. So I just run the check in Am [daily at a specific time for emails, 9am] the script that runs notifies the user that the project is over due or is starting tomorrow. etc

demonz's avatar

@webrobert hi , I have a course table , and for example aws123 course has a start date 20/2/2023 and status upcoming ,then if this date is reached , at 12am it needs to automatically update the status from upcoming to running , how do I achieve that ? task scheduling ? event listener ? i tried cron job but i had a bug when using crontab -e

webrobert's avatar

@demonz it depends on your environment. You mentioned using a cron job. Is it a shared host?

Snapey's avatar

@demonz since the state can't change midway through the day, check it daily with a scheduled task, and fix your crontab issue

webrobert's avatar

I would probably make a command that you can call via the scheduler.

hamzaaslam's avatar
Level 1

To automatically update the course status from "upcoming" to "running" once the start date has been reached, you can use Laravel's task scheduling feature.

Here are the steps you can follow to achieve this:

  1. Create a new console command in Laravel. You can use the following command to create a new console command:
 php artisan make:command UpdateCourseStatus
  1. In the handle() method of the UpdateCourseStatus command, you can write the logic to update the course status. Here's an example:
use App\Models\Course;
use Carbon\Carbon;

public function handle()
{
    $now = Carbon::now();
    $courses = Course::where('start_date', '<=', $now)->where('status', 'upcoming')->get();

    foreach ($courses as $course) {
        $course->status = 'running';
        $course->save();
    }
}

This code retrieves all the courses where the start date is less than or equal to the current date and time and the status is "upcoming". It then updates the status of each course to "running".

  1. In the app/Console/Kernel.php file, add a new entry to the $commands array. The entry should be the class name of the UpdateCourseStatus command.
protected $commands = [
    \App\Console\Commands\UpdateCourseStatus::class,
];
  1. In the schedule() method of the Kernel class, you can schedule the UpdateCourseStatus command to run at 12 am every day. Here's an example:
protected function schedule(Schedule $schedule)
{
    $schedule->command('update-course-status')
             ->dailyAt('12:00');
}
1 like
webrobert's avatar

@hamzaaslam, you could just do...

Course::where('start_date', '<=', now())->where('status', 'upcoming')
     ->update(['status' => 'running']);
2 likes

Please or to participate in this conversation.