Command that scrapes mihaaru
This commit is contained in:
@@ -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"],
|
||||
@@ -53,11 +55,18 @@ class ScrapeMihaaruCommand extends Command
|
||||
"body" => $article["content"],
|
||||
"guid" => $article["guid"],
|
||||
"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);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user