Laravel 5 better way to do this? or place to put this method

I have a RoutesController, to list disponible routes to the user by role:

public function index()
    $routList = [];
    foreach ($this->routes as $route) {
        if($route = $this->userCanAccess($route))
            $routList[] = $route->getUri();
    return response()->json($routList);

public function userCanAccess(Route $route){
    $actions = $route->getAction();
    if ($this->user->role == "root" ||
        !isset($actions['roles']) ||
            && (in_array($this->user->role, $actions['roles']))
    ) {
        return $route;
    return false;

I ended using this userCanAccess method in the Roles controller, but, I thought to put a method in User model like canAccess(Route $route), then I’ll call $this->user->canAccess($route) its the better legible form, but can i made a method like this in the model?

Is there any other way to do that?

last question, if I want to add a method to User object, how I can do this?

note: I want to reuse this method in a middleware too.

Source: laravel

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.