How to get the Model instance from route model binding in Laravel

Categorized as Laravel Tagged ,

This code snippet can be useful in authorizing form requests. For example, you get the post instance from the request and then check the user rights.

// Get the post instance from the request
$request->route('post');

// usage

/**
 * Determine if the user is authorized 
 * to make this request
 * 
 * @return bool
 */

public function authorize()
{
  return $this->user()->can('update', $this->route('post'));
}

Leave a reply

Your email address will not be published. Required fields are marked *