JC_
JC_

Reputation: 577

How to call function when a certain date arrives

I have an API (made with Laravel) for an iOS app that is an events manager where the users have some kind of points for going to the events and if they say they will go but then they do not end up going I have to take out some points.

So my question is how can I call the function below when the event has passed to take out the points.

public function takePoints(){
    
    $postUser = post_user::where('posts_id', '=', $id);

    foreach ($postUser as $post){
        if($post->state){
            $user = User::where('id', '=', $postUser->user_id);
            
            $user->points -= 50;
            
            $user->saveOrFail();
        }
    }
}

So basically something like:

if($post->date = $todaysDate){
  $this->takePoints()
}

Upvotes: 1

Views: 1978

Answers (1)

user320487
user320487

Reputation:

You would use Laravel's task scheduling to run periodic checks and perform an operation when a specified time has passed.

https://laravel.com/docs/5.5/scheduling

This runs on top of cron and allows you to create commands that are run at time periods you define within the console kernel.

// add this to cron to run every minute
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

And from within the kernel you can schedules tasks:

$schedule->job(new Heartbeat)->everyFiveMinutes();

Upvotes: 2

Related Questions