Update
This commit is contained in:
51
app/Console/Commands/PopulateMemeMediaSlugs.php
Normal file
51
app/Console/Commands/PopulateMemeMediaSlugs.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\MemeMedia;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class PopulateMemeMediaSlugs extends Command
|
||||
{
|
||||
protected $signature = 'memes:populate-slugs';
|
||||
|
||||
protected $description = 'Populate slug field for existing MemeMedia records';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$this->info('Starting to populate MemeMedia slugs...');
|
||||
|
||||
$memes = MemeMedia::whereNull('slug')->get();
|
||||
|
||||
if ($memes->isEmpty()) {
|
||||
$this->info('No memes found without slugs.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->info("Found {$memes->count()} memes without slugs.");
|
||||
|
||||
$bar = $this->output->createProgressBar($memes->count());
|
||||
$bar->start();
|
||||
|
||||
foreach ($memes as $meme) {
|
||||
$baseSlug = Str::slug($meme->name);
|
||||
$slug = $baseSlug;
|
||||
$counter = 1;
|
||||
|
||||
// Ensure slug is unique
|
||||
while (MemeMedia::where('slug', $slug)->exists()) {
|
||||
$slug = $baseSlug.'-'.$counter;
|
||||
$counter++;
|
||||
}
|
||||
|
||||
$meme->update(['slug' => $slug]);
|
||||
$bar->advance();
|
||||
}
|
||||
|
||||
$bar->finish();
|
||||
$this->newLine();
|
||||
$this->info('Successfully populated all MemeMedia slugs!');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user