Skip to content

eager loading: $this new model #2878

@theticraft

Description

@theticraft

Description

I wanted to get all users of a role (with the belongsToMany Relationship) and got an error stating Class name must be a valid object or a string. After a bit of debug, I noticed that $this inside the users() function was a completely new model (like when you call new Role()). However, before calling users(), I dumped $role and it was the correct Role with every attribute. By calling $role->users()->get(), all the users were displayed...

Steps To Reproduce

  1. add Model::automaticallyEagerLoadRelationships(); to AppServiceProvider.php
  2. try to access ->users not the function; as a parameter
  3. get Class name must be a valid object or a string, because $this->exists is false, $this->guard_name is sanctum, because it's default, and is a new Role
Image

Version of spatie/laravel-permission package:

6.21.0

Version of laravel/framework package:

v12.28.1

PHP version:

8.2.29

Database engine and version:

MariaDB 10.6.22

OS: Windows/Mac/Linux version:

linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions