【发布时间】:2015-04-17 12:45:58
【问题描述】:
我目前正在研究 FormRequest 对象以使用它来执行传入数据的身份验证和验证。但是,在使用模型注入时,我无法让它工作。
Routes.php:
Route::model('post', 'Post');
Route::model('comment', 'Comment');
Route::resource('post', 'PostController');
Route::resource('post.comments', 'CommentController');
发布请求:
class StoreCommentRequest extends FormRequest {
public function authorize()
{
$post = $this->route('post');
$owners = $report->users;
return $owners->contains(Auth::id());
}
public function rules()
{
return [
'post_id' => 'required|numeric|exists:posts,id'
];
}
}
每当发表评论时,我都会收到消息:
"The post id field is required"
问题是我似乎无法从绑定到路由的 Post 模型中“注入”post_id 的正确值。
是否可以使用路由参数?如果有,怎么做?
【问题讨论】:
-
在 authorize() 函数中执行
dd(\Input::all());并检查天气 id 值是否已分配 -
id 参数不与数据一起发布。相反,我想从路由中检索参数。
-
试试这个:
$post = $this->route('post_id');另外,\Input::all()也应该有来自路由的值 -
post_id是routes.php中定义的URL参数还是表单提交的东西?