From 8c12b94777c5de88622207a18d0998a92488a16b Mon Sep 17 00:00:00 2001 From: Mohamed Jinas Date: Tue, 11 Aug 2020 04:48:09 +0500 Subject: [PATCH] Topic Fix in commands --- app/Console/Commands/ScrapeMihaaruCommand.php | 5 +- app/Console/Commands/ScrapeSunCommand.php | 76 +++++++++++++++++++ .../Commands/ScrapeThiladhunCommand.php | 5 +- 3 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 app/Console/Commands/ScrapeSunCommand.php diff --git a/app/Console/Commands/ScrapeMihaaruCommand.php b/app/Console/Commands/ScrapeMihaaruCommand.php index 6f897d3..9ca85e3 100644 --- a/app/Console/Commands/ScrapeMihaaruCommand.php +++ b/app/Console/Commands/ScrapeMihaaruCommand.php @@ -62,10 +62,7 @@ class ScrapeMihaaruCommand extends Command ]); collect($article["topics"])->each(function($topic) use ($articleModel) { - $topicModel = Topic::firstOrCreate([ - "name" => $topic["name"], - "slug" => $topic["slug"], - ]); + $topicModel = Topic::firstOrCreate(["slug" => $topic["slug"]],["name" => $topic["name"]]); $topicModel->articles()->syncWithoutDetaching($articleModel); }); diff --git a/app/Console/Commands/ScrapeSunCommand.php b/app/Console/Commands/ScrapeSunCommand.php new file mode 100644 index 0000000..d77f284 --- /dev/null +++ b/app/Console/Commands/ScrapeSunCommand.php @@ -0,0 +1,76 @@ +first(); + + $articles = (new SunService)->scrape(); + + 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"], + "featured_image" => $article["image"], + "body" => $article["content"], + "guid" => $article["guid"], + "published_date" => Carbon::parse($article["date"])->format("Y-m-d H:i:s"), + "meta" => [ + "title" => $article["og_title"] + ] + + ]); + + + + collect($article["topics"])->each(function($topic) use ($articleModel) { + + $topicModel = Topic::firstOrCreate(["slug" => $topic["slug"]],["name" => $topic["name"]]); + + $topicModel->articles()->syncWithoutDetaching($articleModel); + }); + + } + } +} diff --git a/app/Console/Commands/ScrapeThiladhunCommand.php b/app/Console/Commands/ScrapeThiladhunCommand.php index 43f60f4..882d427 100644 --- a/app/Console/Commands/ScrapeThiladhunCommand.php +++ b/app/Console/Commands/ScrapeThiladhunCommand.php @@ -63,10 +63,7 @@ class ScrapeThiladhunCommand extends Command ]); collect($article["topics"])->each(function ($topic) use ($articleModel) { - $topicModel = Topic::firstOrCreate([ - "name" => $topic["name"], - "slug" => $topic["slug"], - ]); + $topicModel = Topic::firstOrCreate(["slug" => $topic["slug"]],["name" => $topic["name"]]); $topicModel->articles()->syncWithoutDetaching($articleModel); });