Site icon VR SoftCoder

How to call a method of controller with cron in laravel

How to call a method of controller with cron in laravel, laravel call controller method from command, laravel artisancall controller, laravel custom cron schedule, laravel scheduler, laravel run cron job manually, laravel cron job ubuntu, laravel cron job not working, laravel scheduler multiple commands, How can I call command schedule via url on the laravel, how to call a route with cron in laravel

How to call a method of controller with cron in laravel, laravel call controller method from command, laravel artisancall controller, laravel custom cron schedule, laravel scheduler, laravel run cron job manually, laravel cron job ubuntu, laravel cron job not working, laravel scheduler multiple commands, How can I call command schedule via url on the laravel, how to call a route with cron in laravel

In this tutorial we are going to learn how to call a method of controll through CRON and schedule them. Now create a contoller and a function inside that controller. In this tutorial we will create a user every minute using laravel CRON schedule. So first we will create a usersController. See below example:–

Create a Controller


 'First Name',
                'last_name' => 'Last Name',
                'email' => rand().'user@vrsoftcoder.com',
                'password' => \Illuminate\Support\Facades\Hash::make('laravue'),
            ]);

	     if($user){
	     	echo "User Successfully Created!";
	     }
	}

}

Create a User Model

To insert users we need to create a model and four fields in our users table as describe below:-

 'datetime',
    ];
}

Create a Route

In next step we need to create a route to run the the function. Then we will call this route through CURL in laravel schedule function.

Route::get('/createusers','users@CreateUsers');

Set CRON

In final setup call our route in schedule function like below. CRON will hit this route every minute to create a new user.

call(function () {

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, "http://localhost/blog/public/createusers");
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_exec($ch);
                curl_close($ch);
            })->everyMinute();
    }

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

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

Now run below command to run the CRON:-

php artisan schedule:run

You have successfully create the users through CRON. Check below screenshot:-

If you have any question please comment below.

How to call a method of controller with cron in laravel, laravel call controller method from command, laravel artisan::call controller, laravel custom cron schedule, laravel scheduler, laravel run cron job manually, laravel cron job ubuntu, laravel cron job not working, laravel scheduler multiple commands, How can I call command schedule via url on the laravel, how to call a route with cron in laravel

Exit mobile version