Efficiently\AuthorityController\ControllerAdditions::loadResource PHP Method

loadResource() public method

For example, given an ArticlesController it will load the current article into the @article instance variable. It does this by either calling Article->find($this->params['id']); or new Article($this->params['article']); depending upon the action. The index action will automatically set $this->articles to Article::get(); or Article::$options['collectionScope']()->get(); If a conditional callback is used in the Authority, the 'create' and 'store' actions will set the initial attributes based on these conditions. This way these actions will satisfy the authority restrictions. Call this method directly on the controller class. class BooksController extends Controller { public function __construct() { $this->loadAndAuthorizeResource(); } } A resource is not loaded if the instance variable is already set. This makes it easy to override the behavior through a beforeFilter() on certain actions. class BooksController extends Controller { public function __construct() { $this->beforeFilter('findBookByPermalink', ['only' => 'show']); $this->loadAndAuthorizeResource(); } protected function findBookByPermalink() { $this->book = Book::where('permalink', $this->params['id'])->firstOrFail(); } } If a name is provided which does not match the controller it assumes it is a parent resource. Child resources can then be loaded through it. class BooksController extends Controller { public function __construct() { $this->loadResource('author'); $this->loadResource('book', ['through' => 'author']); } } Here the author resource will be loaded before each action using $this->params['author_id']. The book resource will then be loaded through the $this->author instance variable. That first argument is optional and will default to the singular name of the controller. A hash of options (see below) can also be passed to this method to further customize it. See loadAndAuthorizeResource() to automatically authorize the resource too. Options: ['only'] Only applies before filter to given actions. ['except'] Does not apply before filter to given actions. ['through'] Load this resource through another one. This should match the name of the parent instance variable or method. ['throughAssociation'] The name of the association to fetch the child records through the parent resource. This is normally not needed because it defaults to the pluralized resource name. ['shallow'] Pass true to allow this resource to be loaded directly when parent is null. Defaults to false. ['singleton'] Pass true if this is a singleton resource through a hasOne association. ['parent'] True or false depending on if the resource is considered a parent resource. This defaults to true if a resource name is given which does not match the controller. ['class'] The class to use for the model (string). ['instanceName'] The name of the instance variable to load the resource into. ['findBy'] Find using a different attribute other than id. For example. $this->loadResource(['findBy' => 'permalink']); will use where('permalink', $this->params['id'])->firstOrFail() ['idParam'] Find using a param key other than 'id'. For example: $this->loadResource(['idParam' => 'url']); // will use find($this->params['url']) ['collection'] Specify which actions are resource collection actions in addition to index. This is usually not necessary because it will try to guess depending on if the id param is present. $this->loadResource(['collection' => ['sort', 'list']]); ['create'] Specify which actions are new resource actions in addition to new, create and store. Pass an action name into here if you would like to build a new resource instead of fetch one. $this->loadResource(['create' => 'build']); ['collectionScope'] The name of the query scope to fetch the collection records of collection actions (E.g. index action). $this->loadResource(['collectionScope' => 'scopePopular']); // will use Article::popular()->get(); to fetch records of collection actions You can pass parameters with an array. For example: $this->loadResource(['collectionScope' => ['scopeOfType', 'published']]); // will use Article::ofType('published')->get(); By default, collection actions (index action) returns all the collection record with: Article::get(); // which is equivalent to Article::get(); ['prepend'] Passing true will use prependBeforeFilter() instead of a normal beforeFilter().
public loadResource ( $args = null )
    public function loadResource($args = null)
    {
        $args = is_array($args) ? $args : func_get_args();
        ControllerResource::addBeforeFilter($this, __METHOD__, $args);
    }