In Laravel, I have a table of articles which I pulled from a database at the start of my project over a month ago.
This database is separate from my Laravel application but the content may change daily and I've been manually grabbing the content every three days, which as you can imagine takes time.
I've seen that you can have two database connects in Laravel, like so:
<?php
return array(
'default' => 'mysql',
'connections' => array(
# Our primary database connection
'mysql' => array(
'driver' => 'mysql',
'host' => 'host1',
'database' => 'database1',
'username' => 'user1',
'password' => 'pass1'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Our secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => 'host2',
'database' => 'database2',
'username' => 'user2',
'password' => 'pass2'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
);
So, If I have my secondary article table that I can connect to is it possible to create a cron job that pulls in new content to my Laravel application every hour?
When pulling from the secondary database, how could I avoid overwriting content?
You can define a model for the secondary database like this
namespace App\Secondary;
class Article extends Model {
protected $connection = 'mysql2';
public static function fetchArticles(){
//fetch articles
//TODO all other filter to fetch new records
$articles = Article::all();
return $articles;
}
}
How to avoid overwrite content?
if you have id or any identity column in both primary and secondary connection db table then just fetch the latest article id from primary connection article table and fetch new articles after that id from secondary db table.
Here is the scheduler class
namespace App\Console\Commands;
use Illuminate\Console\Command;
class ArticlePuller extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'articlePuller';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Pull article from secondary database';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$articles = Secondary/Article::fetchArticles(); //from secondary db table
Article::insertArticles($articles);
}
}
Define this scheduler in console/Kernel.php
protected $commands = [
Commands\ArticlePuller::class
];
protected function schedule(Schedule $schedule)
{
$schedule->command('articlePuller')->hourly();
}
Now need to add this scheduler entry in cron job
* * * * * php path_to_artisan/artisan schedule:run >> /dev/null 2>&1