diff --git a/app/Models/Recipe.php b/app/Models/Recipe.php index 1e57bc4..b7250ff 100755 --- a/app/Models/Recipe.php +++ b/app/Models/Recipe.php @@ -9,10 +9,14 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Contracts\Database\Eloquent\Builder; +use Spatie\Sluggable\HasSlug; +use Spatie\Sluggable\SlugOptions; class Recipe extends Model implements Viewable { - use HasFactory, InteractsWithViews; + use HasFactory; + use InteractsWithViews; + use HasSlug; protected $casts = [ 'steps' => AsCollection::class, @@ -24,6 +28,27 @@ class Recipe extends Model implements Viewable 'comments', ]; + public function getSlugOptions(): SlugOptions + { + return SlugOptions::create() + ->generateSlugsFrom(['title']) + ->saveSlugsTo('slug') + // ->slugsShouldBeNoLongerThan(20) + ->usingSeparator('_'); + // ->usingLanguage('ru') + // ->doNotGenerateSlugsOnUpdate(); + } + + /** + * Get the route key for the model. + * + * @return string + */ + public function getRouteKeyName() + { + return 'slug'; + } + public function tags() { return $this->morphToMany(Tag::class, 'taggable'); diff --git a/database/migrations/2023_12_25_123200_add_slug_to_recipe.php b/database/migrations/2023_12_25_123200_add_slug_to_recipe.php new file mode 100644 index 0000000..0b3b89f --- /dev/null +++ b/database/migrations/2023_12_25_123200_add_slug_to_recipe.php @@ -0,0 +1,28 @@ +string('slug'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('recipes', function (Blueprint $table) { + $table->dropColumn('slug'); + }); + } +};