Laravel 5.1 - 如何在Request authorize()方法中访问资源ID

时间:2015-09-19 05:30:53

标签: php laravel

我正在做房地产申请。当用户打开编辑表单以编辑其中一个属性时,刀片代码将为:

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

正如您所看到的,当前正在编辑的属性的ID是最后一个数组元素。

如何在请求的authorize()函数中访问该ID?

class EditPropertyRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Property::where('user_id', auth()->user()->id)
                         ->where('id', <<< HERE IS WHERE I NEED THE ID FROM THE FORM >>)
                         ->exists();
    }
    // ...
}

1 个答案:

答案 0 :(得分:1)

public function authorize()
                         ^ //add $id here
{
    return Property::where('user_id', auth()->user()->id)
                     ->where('id', <<< HERE IS WHERE I NEED THE ID FROM THE FORM >>)
                     ->exists();
}