uzhas
uzhas

Reputation: 925

How to execute certain function after some time?

I have this function where user can reject other user.

 public function rejectPersonalUser($id){
        $user = PersonalUser::findOrFail($id);
        $user->approved = 0;
        $user->business_user_id = NULL;
        $user->save();

        return redirect()->back()->withFlashMessage('User has been rejected successfully!!');
   }

Now what i want is to check if user reject other user after 7 days if its not to call other function.

   public function setUser($id){
    $user = PersonalUser::findOrFail($id);
     if($user->approved == 0 && $user->business_user_id != NULL){
        $user->approved = 0;
        $user->business_user_id = NULL;
        $user->save();
    }
 }

Upvotes: 2

Views: 3105

Answers (1)

PaladiN
PaladiN

Reputation: 4974

You can use Task Scheduling, If you want it to run after some time period.

You could just use like:

$schedule->call(function () {
    //your block of codes here..
})->weekly()->mondays()->at('13:00');

Or more specifically for your needs you could call to a function that doesn't inject any parameters and try to call the user from there.

$schedule->call(function () {
 //call someScheduleTaskForUser() from here.
})->weekly()->mondays()->at('13:00');


public function someScheduleTaskForUser() {
  //call  the setUser() function from here...
}

// This function could reside somewhere.
 public function setUser($id){
    $user = PersonalUser::findOrFail($id);
     if($user->approved == 0 && $user->business_user_id != NULL){
        $user->approved = 0;
        $user->business_user_id = NULL;
        $user->save();
    }
 }

Upvotes: 3

Related Questions