nova-permissions icon indicating copy to clipboard operation
nova-permissions copied to clipboard

Roles resource does not work when preventLazyLoading is true

Open webard opened this issue 2 years ago • 1 comments

Illuminate\Database\LazyLoadingViolationException
Attempted to lazy load [permissions] on model [App\Models\Role] but lazy loading is disabled.

In AppServiceProvider.php:

use Illuminate\Database\Eloquent\Model;
...
public function register(): void {
	Model::preventLazyLoading(true);
}

webard avatar Jun 23 '23 03:06 webard

It's necessary lazy loading enabled to load role permissions. If you known another way, please make a PR.

leandrogehlen avatar Jun 23 '23 12:06 leandrogehlen

Can't we simply use protected $with = ['permissions']; in the Role model ?

mathieufrh avatar Jul 31 '24 08:07 mathieufrh

Yes this solve it. But, every query will load the permissions relation. I'm not sure if this is necessary in every query.

ref #50

leandrogehlen avatar Jul 31 '24 10:07 leandrogehlen

@leandrogehlen your concern is legit! But it is only on index queries.

Please take a look at vendor/laravel/nova/src/Resource.php:88. It says The relationships that should be eager loaded when performing an index query.

It is used only here: \Laravel\Nova\PerformsQueries::buildIndexQuery.

I will add the $with to the MR as we had in the previous one.

HenriqueSPin avatar Jul 31 '24 13:07 HenriqueSPin

I tested with Preventing Lazy Loading active and confirmed the changes at #50 fixed the issue.

HenriqueSPin avatar Jul 31 '24 14:07 HenriqueSPin