Command that scrapes mihaaru

This commit is contained in:
2020-08-10 06:18:09 +05:00
parent 3e69fded50
commit fa33aa2e8b

View File

@@ -5,6 +5,7 @@ namespace App\Console\Commands;
use App\Source;
use Illuminate\Console\Command;
use App\Services\MihaaruService;
use App\Topic;
class ScrapeMihaaruCommand extends Command
{
@@ -43,9 +44,10 @@ class ScrapeMihaaruCommand extends Command
$articles = (new MihaaruService)->scrape();
foreach($articles as $article)
{
$source->articles()->firstOrCreate([
foreach ($articles as $article) {
// Attach the relationship between source and article and return the curren article instance
$articleModel = $source->articles()->firstOrCreate([
"title" => $article["title"],
"url" => $article["url"],
"author" => $article["author"],
@@ -55,9 +57,16 @@ class ScrapeMihaaruCommand extends Command
"published_date" => $article["date"],
]);
}
collect($article["topics"])->each(function($topic) use ($articleModel) {
$topicModel = Topic::firstOrCreate([
"name" => $topic["name"],
"slug" => $topic["slug"],
]);
$topicModel->articles()->syncWithoutDetaching($articleModel);
});
}
}
}