statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
4.04k stars 530 forks source link

Users Eloquent Driver N+1 query problem (follow up) #3535

Closed vguerrerobosch closed 3 years ago

vguerrerobosch commented 3 years ago

This is a follow up on https://github.com/statamic/cms/issues/1511

Bug Description

When Storing Users in a Database, N+1 query problems show up here and there.

How to Reproduce

Fresh install Laravel + Statamic + Debugbar. I just published all the scafold in this repo for convinience if you like. Just clone, setup your env including database, and php artisan migrate:fresh --seed. You will have a super user with admin@statamic.test and password as password.

Extra Detail

Login to the control panel and Laravel Debugbar will show like 50ish duplicate queries

select * from `users` where `id` = 1 limit 1
select * from `group_user` where `user_id` = 1

In AuthServiceProvider you will find a couple of Gates like:

Gate::define('do-something', function (User $user) {
    return $user->super = true;
});

Gate::define('do-something-else', function (User $user) {
    return $user->super = true;
});

then in post.blade.php you'll find @can directives using those gates, that also generate duplicate queries.

Environment

Statamic version: 3.1.6

PHP version: 7.4

Install method (choose one): fresh Laravel + install Statamic + Laravel Debugbar

duncanmcclean commented 3 years ago

The group_user issue here should be fixed by #3527.

jasonvarga commented 3 years ago

Fixed in #3527