entrust icon indicating copy to clipboard operation
entrust copied to clipboard

Verifying user roles multiple times in the blade template, doing the same select from database

Open mkuzmych opened this issue 7 years ago • 14 comments

I use check: @if(Auth::user()->hasRole(['administrator']))...@endif multiple time at the same template, i get 10 selects from database select roles.*, role_user.user_id as pivot_user_id, role_user.role_id as pivot_role_id from roles inner join role_user on roles.id = role_user.role_id where role_user.user_id = '1'. Can I do this select once time? P.S.: I check user roles in different place and cannot group them. Thanks!!

mkuzmych avatar Nov 08 '17 13:11 mkuzmych

I'm facing exact same problem.

dneykov avatar Nov 13 '17 07:11 dneykov

That is a problem for sure. I will be looking into it

Zizaco avatar Nov 13 '17 23:11 Zizaco

Hi @Zizaco

I am having the same problem too.

For a simple page I have

620 statements were executed, 617 of which were duplicated, 3 unique

It is killing the server

Duplicates are roles and permissions:

select roles.*, role_user.user_id as pivot_user_id, role_user.role_id as pivot_role_id, role_user.created_at as pivot_created_at, role_user.updated_at as pivot_updated_at from roles inner join role_user on roles.id = role_user.role_id where role_user.user_id = '2'

select permissions.*, permission_role.role_id as pivot_role_id, permission_role.permission_id as pivot_permission_id from permissions inner join permission_role on permissions.id = permission_role.permission_id where permission_role.role_id = '2'

I attached a screenshot from debugger. entrust-bug

anis-bedhiafi avatar Nov 14 '17 17:11 anis-bedhiafi

@anis-bedhiafi you should take a look to your queries. Maybe you might have some duplicates. I was facing the same issue but after revise my code and optimized it, i got rid of some duplicate queries.

In addition to this, i also follow the steps described here and this solved my issue. You need to use cache in order to avoid queries to be performed on every page load. Take a look to the comment by @big-zein here .

Add the following to config/cache.php:

'ttl' => 30 //In minutes

Let me know if this solves your problem.

carlosadames avatar Nov 14 '17 17:11 carlosadames

@uapixart, take a look to my previous comment and let me know if it helps.

carlosadames avatar Nov 14 '17 17:11 carlosadames

@carlosadames

Thanks for the reply.

This is surely a problem from Entrust itself and not my queries.

Look here I am rendering an empty page with no queries to the database.

class UserController extends Controller { public function index() { return view('user.dashboard'); } }

15 statements were executed, 14 of which were duplicated, 1 unique

Screenshot en

anis-bedhiafi avatar Nov 14 '17 18:11 anis-bedhiafi

@carlosadames It does not cache the queries too. Got the very exact problem.

jhemmmm avatar Nov 15 '17 14:11 jhemmmm

@carlosadames, Dont work, this problem with module Zizaco/entrust, with must get user with roles first and then make select from local and not from database. Thanks!

mkuzmych avatar Nov 15 '17 14:11 mkuzmych

@Zizaco Any updates on this bug? Should we consider other alternatives?

Thank you.

anis-bedhiafi avatar Nov 21 '17 13:11 anis-bedhiafi

I was facing the same problem and after setting up 'ttl' => 30 //In minutes in config/cache.php, redis as cache driver and then running php artisan config:cache helped me to get rid of this problem. Previously it was extra 140+ queries due to dynamic menu as per permission and was causing me almost 1.5 secs delay in per page load. Taking a look at EntrustUserTrait (vendor > zizaco > entrust >src > Entrust > Traits > EntrustUserTrait.php) could be helpful to know how it works behind the scene.

git-mahfuz avatar Dec 04 '17 05:12 git-mahfuz

same issue on laravel 5.4

dflow avatar Dec 14 '17 09:12 dflow

In my case, add 'ttl' => 60 in app/cache.php solves the problem.

And I just made a new PR #908 to add new config to be used in cachedRoles and cachedPermissions functions, so we don't need to add new config in app/cache.php manually

eantz avatar Feb 27 '18 14:02 eantz

@eantz what is setting ttl doing and what will it have an effect on besides entrust?

dflow avatar Apr 17 '18 17:04 dflow

@dflow I think this https://github.com/Zizaco/entrust/issues/871#issuecomment-348867290 already explained it.

eantz avatar Apr 18 '18 13:04 eantz