I have a section in my project with the latest news articles. For this I have a:
- Post model
- Post resource controller and a
- Resource Post Route.
Post Model
class Post extends Model
{
use HasFactory, Sluggable;
protected $fillable = [...,...];
public function getRouteKeyName()
{
return 'slug';
}
public function sluggable(): array
{
return [
'slug' => [
'source' => 'title'
]
];
}
}
PostController.php
public function show(Post $post)
{
dd($post);
}
web.php
Route::resource('/posts', App\Http\Controllers\PostController::class)->only(['index','show']);
Index (http://localhost/news
) and show (http://localhost/news/{slug}
) work as expected!
Now the problem/bug I noticed:
When I change the route from posts to news, the show method no longer works. Index still works.
the modified route from posts to news
Route::resource('/news', App\Http\Controllers\PostController::class)->only(['index','show']);
http://localhost/news
works but http://localhost/news/{slug}
shows me only the PostModel Structure.
Do you know the problem and what do I have to do to make it work? I use Laravel 8 and "cviebrock/eloquent-sluggable": "^8.0" package
for the slugs. Thanks for your time!