menu

arrow_back How do I write gates compactly in Laravel?

by
2 votes
I use gates to check the permissions. If you define each permission this way, you get a big function. How can I avoid it?
public function boot()
{
$this->registerPolicies();

Gate::define('users_info', function ($admin) {
if (! $admin->hasPermission('users_info') ) {
throw new AccessDenyException(ErrorMessages::ACCESS_DENIED, Response::HTTP_FORBIDDEN);
};

return true;
});
}

2 Comments

Start using policies?
JhaoDa I don't know why I'm stuck with gates, it's really better to use them, thanks.

1 Answer

by
0 votes
There are ready-made packages. Why all this misery?

4 Comments

Sanes I don't need those tables, just a table with permissions and one summary. The haspermission method and the policy that calls it. Used to use entrust, wouldn't say there's less to write
These packages create several tables in the database and add a bunch of code, and so I added the hasPermission method for the model and call it in the policy.
Eugene , they create it because you have to store it somewhere.
Eugene I don't need it right now. I use laravel-permission. You can disable permissions and leave only roles.