芝麻web文件管理V1.00
编辑当前文件:/home/qrafawbu/rentandbuyrealty.com/rentandbuyrealty.com/app/Console/Commands/SetSlugs.php
setUniqueSlugs(Property::class, 'title'); $this->setUniqueSlugs(Category::class, 'category'); $this->setUniqueSlugs(Article::class, 'title'); $this->info('Slug values set for existing records.'); } private function setUniqueSlugs($modelClass, $titleColumn) { $records = $modelClass::all(); foreach ($records as $record) { $originalSlug = Str::slug($record->$titleColumn); $slug = $this->generateUniqueSlug($modelClass, $originalSlug, $record->id); $record->slug_id = $slug; $record->save(); } } private function generateUniqueSlug($modelClass, $originalSlug, $recordId = null) { $slug = $originalSlug; $existingRecords = $modelClass::where('slug_id', $slug); if ($recordId) { $existingRecords->where('id', '!=', $recordId); } $counter = 1; while ($existingRecords->count() > 0) { $slug = $originalSlug . '-' . $counter; $existingRecords = $modelClass::where('slug_id', $slug); if ($recordId) { $existingRecords->where('id', '!=', $recordId); } $counter++; } return $slug; } }