【问题标题】:Laravel 5.1 - How to access resource id in a Request authorize() methodLaravel 5.1 - 如何在 Request authorize() 方法中访问资源 id
【发布时间】:2015-09-19 13:45:06
【问题描述】:

我正在申请房地产。当用户打开编辑表单来编辑他的一个属性时,刀片代码将是:

{!! 
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();
    }
    // ...
}

【问题讨论】:

  • public function authorize($id)
  • 我可以将此作为答案并接受吗? :D
  • 哈哈,去吧,把答案放上去
  • 啊哈哈。肿瘤坏死因子。您忘记将$id 放在那里。赶上你通过的id。 :D

标签: php laravel


【解决方案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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-08
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多