Topic Fix in commands
This commit is contained in:
		@@ -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);
 | 
			
		||||
            });
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										76
									
								
								app/Console/Commands/ScrapeSunCommand.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								app/Console/Commands/ScrapeSunCommand.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,76 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Console\Commands;
 | 
			
		||||
 | 
			
		||||
use App\Source;
 | 
			
		||||
use Illuminate\Console\Command;
 | 
			
		||||
use App\Services\SunService;
 | 
			
		||||
use App\Topic;
 | 
			
		||||
use Illuminate\Support\Carbon;
 | 
			
		||||
 | 
			
		||||
class ScrapeSunCommand extends Command
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * The name and signature of the console command.
 | 
			
		||||
     *
 | 
			
		||||
     * @var string
 | 
			
		||||
     */
 | 
			
		||||
    protected $signature = 'scrape:sun';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The console command description.
 | 
			
		||||
     *
 | 
			
		||||
     * @var string
 | 
			
		||||
     */
 | 
			
		||||
    protected $description = 'Scrap sun mv';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Create a new command instance.
 | 
			
		||||
     *
 | 
			
		||||
     * @return void
 | 
			
		||||
     */
 | 
			
		||||
    public function __construct()
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Execute the console command.
 | 
			
		||||
     *
 | 
			
		||||
     * @return int
 | 
			
		||||
     */
 | 
			
		||||
    public function handle()
 | 
			
		||||
    {
 | 
			
		||||
        $source = Source::where('slug', 'sun')->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);
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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);
 | 
			
		||||
            });
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user