Command that scrapes mihaaru
This commit is contained in:
		| @@ -5,6 +5,7 @@ namespace App\Console\Commands; | |||||||
| use App\Source; | use App\Source; | ||||||
| use Illuminate\Console\Command; | use Illuminate\Console\Command; | ||||||
| use App\Services\MihaaruService; | use App\Services\MihaaruService; | ||||||
|  | use App\Topic; | ||||||
|  |  | ||||||
| class ScrapeMihaaruCommand extends Command | class ScrapeMihaaruCommand extends Command | ||||||
| { | { | ||||||
| @@ -43,9 +44,10 @@ class ScrapeMihaaruCommand extends Command | |||||||
|  |  | ||||||
|         $articles = (new MihaaruService)->scrape(); |         $articles = (new MihaaruService)->scrape(); | ||||||
|  |  | ||||||
|         foreach($articles as $article) |         foreach ($articles as $article) { | ||||||
|         { |  | ||||||
|             $source->articles()->firstOrCreate([ |             // Attach the relationship between source and article and return the curren article instance | ||||||
|  |             $articleModel = $source->articles()->firstOrCreate([ | ||||||
|                 "title" => $article["title"], |                 "title" => $article["title"], | ||||||
|                 "url" => $article["url"], |                 "url" => $article["url"], | ||||||
|                 "author" => $article["author"], |                 "author" => $article["author"], | ||||||
| @@ -55,9 +57,16 @@ class ScrapeMihaaruCommand extends Command | |||||||
|                 "published_date" => $article["date"], |                 "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