TuffRivers
6 months ago
763
5
Laravel

Possible for Laravel Policy without $arguments?

Posted 6 months ago by TuffRivers

I have a default policy that uses a constructor to inject the authenticated user. That means for some controllers, i have no arguments to pass the $this->authorize('action', $arguments);

for example

    public function index() //nothing here
    {
        
        $this->authorize('index'); //only admins can get ALL (returns for all clients) 

        return $this->sendResponse(Card::All());

    }

DefaultPolicy

 
public function __construct(){
        
     $this->user = Auth::user();  //get current auth user
        
    }
    
    public function index()
    {
        return $this->user->role == 'admin'; //if admin return true
    }

The issue is, if i dont pass any arguments, it fails at $this->authorize('action'); and wont go to the policy at all.

I can pass an empty variable but that seems hack, is there a better way?

Please sign in or create an account to participate in this conversation.