Laravel: Cron Job Task Scheduling in order to Store Data from API Call in Database

Laravel Cron jobs, DB, Api

How to Create a Cron Job?

php artisan make:command CakeCron — command=cake:cron
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class CakeCron extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'cake:cron'; /**
* The console command description.
*
* @var string
*/
protected $description = 'Cake Command Executed Successfully!';
//Description will be shown when the php artisan list command is executed.
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
\Log::info("Cake Cron execution!");
$this->info('Cake:Cron Command is working fine!');
}}
<?phpnamespace App\Console;use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
\App\Console\Commands\CakeCron::class,
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('cake:cron')
->hourly();
} /**
* Register the Closure based commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
php artisan schedule:runORphp artisan cake:cron
php artisan list

Task Scheduling

Starting the Laravel SchedulerGo to your terminal, cd into your project and run this command: crontab -eafter that press i in your keyboard in order to insert data, then paste the command below:* * * * * php /path-to-project/artisan schedule:run 1>> /dev/null 2>&1after that press ESC button and write :wq (if you want to save the changes)!!!*TIP*!!!: To get full path of your project, run pwd command.

Create a new Table in our Database

php artisan make:migration create_cake --create=cake
<?phpuse Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCake extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('cake', function (Blueprint $table) {
$table->bigIncrements('id');
$table->json('name')->nullable();
$table->dateTime('created_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::create('cake', function (Blueprint $table) {
$table->dropColumn('id');
$table->dropColumn('name');
$table->dropColumn('created_at');
});
}
}
php artisan migrate

API Call & add data to Database

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$client = new \GuzzleHttp\Client();
$request = $client->get('https://url-to-api');
$response = $request->getBody()->getContents();
$cake = json_decode($response, true);
}
$published_at = \Carbon\Carbon::now();\DB::table(‘cake’)->insert([
[“json_data” => $response, “created_at” => “$published_at”]
]);

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store