Roles resource does not work when preventLazyLoading is true
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);
}
It's necessary lazy loading enabled to load role permissions. If you known another way, please make a PR.
Can't we simply use protected $with = ['permissions']; in the Role model ?
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 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.
I tested with Preventing Lazy Loading active and confirmed the changes at #50 fixed the issue.