我的项目中有一个包含最新新闻文章的部分。为此,我有一个:
- 后模型
- 发布资源控制器和一个
- 资源发布路线。
后模型
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);
}
网页.php
Route::resource('/posts', App\Http\Controllers\PostController::class)->only(['index','show']);
Index ( http://localhost/news
) 和 show ( http://localhost/news/{slug}
) 按预期工作!
现在我注意到的问题/错误:
当我将路线从posts更改为news时, show 方法不再有效。索引仍然有效。
从帖子到新闻的修改路线
Route::resource('/news', App\Http\Controllers\PostController::class)->only(['index','show']);
http://localhost/news
有效,但http://localhost/news/{slug}
只向我展示了 PostModel 结构。
您知道问题所在吗?我该怎么做才能让它发挥作用?我使用 Laravel 8 和"cviebrock/eloquent-sluggable": "^8.0" package
蛞蝓。谢谢你的时间!