-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lazy Loading #2771
Comments
Hi, Feel free to make a PR with the fix I did run this test, and it is working as expected /** @test */
public function it_can_be_given_a_permission_when_prevent_lazy_loading()
{
Model::preventLazyLoading();
$testUserRole = app(Role::class)->where('name', 'testRole')->first();
$testUserRole->givePermissionTo('edit-articles');
$this->assertTrue($testUserRole->hasPermissionTo('edit-articles'));
} |
This test is more specific about this issue...
|
If you are going to do it that way you should use -$roles = app(Role::class)->whereIn("name", ["testUserRole", "anotherTestUserRole"])->get();
+$roles = app(Role::class)->with(['permissions'])->whereIn("name", ["testUserRole", "anotherTestUserRole"])->get(); |
Yes, this is actually intentional. What's your app-specific reason for wanting to prevent lazy loading in your app? Is it related to a specific performance problem? Is it really just for a certain set of models? |
Yeah... But... However, I have to write it like this instead: Here, the call to ->load('permissions') feels unnecessary because it isn’t directly related to assigning the permission. Maybe we could handle this by using $this->loadMissing('permissions') inside the givePermissionTo function to avoid the need for explicit loading in such cases or may be something else. Note: I don’t have a deep understanding of the internals of spatie/laravel-permissions; I’m just speaking from my imagination. If I’ve misunderstood something, please feel free to correct me. |
Yes, I understand your point. I'm also aware that over the years of this package's evolution (since long before Model::preventLazyLoading() existed, and other Laravel improvements that have come over time) we've gone back and forth with explicitly loading, lazy-loading, using loadMissing(), and unsetting, etc. And every time we make the change to accommodate someone's report of the proposed change either improving performance (fewer queries) or fixing intermittent in-memory issues (due to outdated loaded relations), etc etc, it's broken other people's installations or caused ripple-effect bugs. /cc: @erikn69 thoughts? Nevertheless, of course, as you say, if you enable Model::preventLazyLoading(), it's going to affect everything that lets it be affected. And we'll probably need to do something to address that. That's why I asked the question: Why are you explicitly choosing to add Model::preventLazyLoading() to your app? What's your purpose behind that? |
Without restricting lazy loading, I feel like I’m knowingly allowing something potentially harmful to happen that could slow down my app in the future. By enabling Model::preventLazyLoading(), I can proactively catch and address these issues, ensuring better performance and more efficient code. |
Hi, at the moment you could use use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\LazyLoadingViolationException;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
public function boot()
{
Model::preventLazyLoading();
Model::handleLazyLoadingViolationUsing(function (Model $model, string $relation) {
if (in_array(get_class($model), [Permission::class, Role::class])) {
// or // if (in_array($relation, ['roles', 'permissions'])) {
return;
}
throw new LazyLoadingViolationException($model, $relation);
});
} I have no experience with
You could also try, if it works, make a PR |
I don't know how you get that problem, I'm testing and I don't have any code that does that, when editing the role permissions I bring the specific role and assign its permissions ( |
Follow the description please... |
in step 4 you must add if something is added to allow a lazy load in a I mean $roles = Role::where("name", "ARole")->get(); //->with('permissions') fix it
$roles->each/*->filter,->firstWhere,....*/(function ($role) {
$role->givePermissionTo("Can Do Something"); //would run a select query for every role here, N+1??
}); but I did find a problem on this, but different from the one mentioned @drbyte thoughts? N+1 must be allowed, or should |
Description
The current version of spatie/laravel-permission is raising lazy loading exceptions in Laravel. This occurs when
$role->givePermissionTo("Do Something")
. I didn't check for others functions.Steps To Reproduce
Model::preventLazyLoading();
Permission::firstOrCreate(["name" => "Can Do Something"])
Role::firstOrCreate(["name" => "ARole"]);
$roles = Role::where("name", "ARole")->get();
$role = $roles->firstWhere("name", "ARole");
$role->givePermissionTo("Can Do Something");
Example Application
No response
Version of spatie/laravel-permission package:
6.10.1
Version of laravel/framework package:
11.34.2
PHP version:
8.3.14
Database engine and version:
No response
OS: Windows/Mac/Linux version:
No response
The text was updated successfully, but these errors were encountered: