Добавим метод для получения тегов и категории в app/Models/Post.php:
public function tags()
{
return $this->belongsToMany(Tag::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
И для получения постов в app/Models/Tag.php :
public function posts()
{
return $this->belongsToMany(Post::class);
}
И в app/Models/Category.php :
public function posts()
{
return $this->hasMany(Post::class);
}
Слаги
Установим пакет cviebrock/eloquent-sluggable :
composer require cviebrock/eloquent-sluggable
Добавим пространство имен в наши модели:
use Cviebrock\EloquentSluggable\Sluggable;
Добавим в класс каждой модели трейт Sluggable :
use Sluggable;
И метод sluggable:
/**
* Return the sluggable configuration array for this model.
*
* @return array
*/
public function sluggable(): array
{
return [
'slug' => [
'source' => 'title'
]
];
}
Этот метод будет создавать уникальный slug из title для каждой модели.