forked from eatvio/eatvio-infra
added slug to recipe
This commit is contained in:
@@ -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');
|
||||
|
||||
28
database/migrations/2023_12_25_123200_add_slug_to_recipe.php
Normal file
28
database/migrations/2023_12_25_123200_add_slug_to_recipe.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('recipes', function (Blueprint $table) {
|
||||
$table->string('slug');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('recipes', function (Blueprint $table) {
|
||||
$table->dropColumn('slug');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user