Laravel 5.1 - 如何向authorize()函数传递参数

6

我正在制作一个房地产应用程序。当用户打开其中一条已发布的房产进行编辑时,在编辑页面,表单会像这样打开:

{!! Form::model($property, ['method'=>'PUT', 'route'=>['property.update', 'id'=>$property->id], 'files'=>true]) !!}

正如您所见,在“route”数组中,我正在发送命名路由和要编辑的属性的id。但是,我如何在请求类中访问该$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();
    }
}

我得到的错误是
缺少参数 1 用于 App\Http\Requests\EditPropertyRequest::authorize()
2个回答

8

这是来自文档的内容

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}');


1
好的,现在我明白了,你不传递参数,而是使用路由方法。它完美地运行了。谢谢。 - Juan Bonnett

0
request('id')

你可以使用 request();


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接