Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.1 - How to send parameter to authorize() function

I'm making a real estate application. When the user opens one of his published properties to edit, in the edit page, the form opens like this:

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

As you can see, in the 'route' array I'm sending the named route and the id of the property to be edited. But how do I access that $id in the Request Class?

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

The error I get is

Missing argument 1 for App\Http\Requests\EditPropertyRequest::authorize()

like image 367
Juan Bonnett Avatar asked Sep 26 '15 22:09

Juan Bonnett


1 Answers

This is from doc

public function authorize()
{
    $commentId = $this->route('comment');

    return Comment::where('id', $commentId)
                  ->where('user_id', Auth::id())->exists();
}

So $this->route('comment'); is a URL parameter Route::post('comment/{comment}');

like image 56
mcklayin Avatar answered Oct 21 '22 03:10

mcklayin