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

How to call a method of controller 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


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class users extends Controller
{

	public function CreateUsers(){
		
		 $user = User::create([
                'first_name' => '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:-

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name', 'last_name','email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => '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.

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Http\Controller\users;
class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {

        $schedule->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:-

users-created

If you have any question please comment below.

Post Created 43

Leave a Reply

Related Posts

Begin typing your search above and press enter to search. Press ESC to cancel.

Back To Top