Laravel 5.1 - 如何将参数发送到 authorize() 函数
Laravel 5.1 - How to send parameter to authorize() function
我正在申请房地产。当用户打开他发布的属性之一进行编辑时,在编辑页面中,表单打开如下:
{!! Form::model($property, ['method'=>'PUT', 'route'=>['property.update', 'id'=>$property->id], 'files'=>true]) !!}
如您所见,在 'route' 数组中,我发送了命名路由和要编辑的 属性 的 ID。但是我如何在请求 Class?
中访问那个 $id
class EditPropertyRequest extends Request
{
/**
* Determine if the user owns this property and is authorized to make this request.
*
* @return bool
*/
public function authorize($id)
{
return Property::where('user_id', auth()->user()->id)
->where('id', $id)
->exists();
}
}
我得到的错误是
Missing argument 1 for
App\Http\Requests\EditPropertyRequest::authorize()
这是来自文档
public function authorize()
{
$commentId = $this->route('comment');
return Comment::where('id', $commentId)
->where('user_id', Auth::id())->exists();
}
所以$this->route('comment');
是一个URL参数Route::post('comment/{comment}');
request('id')
您可以使用 request();
我正在申请房地产。当用户打开他发布的属性之一进行编辑时,在编辑页面中,表单打开如下:
{!! Form::model($property, ['method'=>'PUT', 'route'=>['property.update', 'id'=>$property->id], 'files'=>true]) !!}
如您所见,在 'route' 数组中,我发送了命名路由和要编辑的 属性 的 ID。但是我如何在请求 Class?
中访问那个 $idclass EditPropertyRequest extends Request
{
/**
* Determine if the user owns this property and is authorized to make this request.
*
* @return bool
*/
public function authorize($id)
{
return Property::where('user_id', auth()->user()->id)
->where('id', $id)
->exists();
}
}
我得到的错误是
Missing argument 1 for App\Http\Requests\EditPropertyRequest::authorize()
这是来自文档
public function authorize()
{
$commentId = $this->route('comment');
return Comment::where('id', $commentId)
->where('user_id', Auth::id())->exists();
}
所以$this->route('comment');
是一个URL参数Route::post('comment/{comment}');
request('id')
您可以使用 request();