Laravel retrieve binded model in Request

Absolutely! It’s an approach I even use myself.

You can get the current route in the request, and then any parameters, like so:

class UpdateRequest extends Request
{
    public function authorize()
    {
        // Get bound Booking model from route
        $booking = $this->route('booking');

        // Eager-load owner relation if it’s not loaded already
        $booking->loadMissing('owner');

        return (string) $booking->owner->user_id === (string) $this->user()->getKey();
    }
}

Unlike smartman’s (now deleted) answer, this doesn’t incur another find query if you have already retrieved the model via route–model binding.

However, I’d also personally use a policy here instead of putting authorisation checks in form requests.


Once you did your explicit binding (https://laravel.com/docs/5.5/routing#route-model-binding) you actually can get your model directly with $this.

class UpdateRequest extends Request
{
    public function authorize()
    {
        return $this->booking->owner->user_id == $this->booking->user()->id;
    }
}

Even cleaner!


To add on to Martin Bean's answer, you can access the bound instance using just route($param):

class UpdateRequest extends Request
{
    public function authorize()
    {
        $booking = $this->route('booking');

        return $booking->owner->user_id == $this->user()->id;
    }
}

Note: This works in Laravel 5.1. I have not tested this on older versions.