【发布时间】:2019-09-26 10:54:12
【问题描述】:
我是 laravel 的新手。你能帮助我吗?我不明白问题出在哪里:
参数 2 传递给 App\Http\Controllers\Posts\PostsController::show() 必须是一个实例 App\Post 的,给定的字符串
后控制器
public function show( Category $category , Post $post )
{
return view('posts.post',[
'post' => $post,
]);
}
模型 Post.php
class Post extends Model
{
protected $fillable = ['user_id','category_id','name','slug','body'];
protected $with = ['user','category'];
protected static function boot()
{
parent::boot(); // TODO: Change the autogenerated stub
self::creating(function($model){
$model->slug = str::slug($model->name);
});
}
public function path()
{
return "/blog/{$this->category->slug}/{$this->id}";
}
public function user ()
{
return $this->belongsTo(User::class);
}//
public function category ()
{
return $this->belongsTo(category::class);
}
}
Web.php
route::group(['namespace'=>'Posts','prefix' => 'blog'], function (){
route::get('','PostsController@index')->name('blog');
route::get('{Category}','PostsController@index');
route::get('{Category}/{post}','PostsController@show')->name('post');
});
【问题讨论】: