-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Role::getTable() method brokes withCount method #2277
Comments
Feel free to open a PR |
xenaio-daniil
added a commit
to xenaio-daniil/laravel-permission
that referenced
this issue
Dec 20, 2022
drbyte
pushed a commit
that referenced
this issue
Feb 8, 2023
* Fix Role::withCount if belongsToMany declared fixes #2277
wakeup0706
added a commit
to wakeup0706/laravel-permission
that referenced
this issue
Feb 13, 2025
* Fix Role::withCount if belongsToMany declared fixes spatie/laravel-permission#2277
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
I needed to get ManyToMany relation of roles (structure of departments). To do this, I extended the
\Spatie\Permission\Models\Role
class with methods:and created migration:
Everything works well except methods like
and so on: it always returns
parent_count = 0
.The reason of this behavior is in overriding of method
getTable
inSpatie\Permission\Models\Role
.When extending
Illuminate\Database\Eloquent\Model
:When extending
\Spatie\Permission\Models\Role
:withCount()
usesModel::table
field to temporary store alias name (laravel_reserved_0
).And when (in
Illuminate\Database\Eloquent\Model
) methodModel::getTable()
returns$this->table
or magic name from class basename, your version returns only pre-configurated table name (config('permission.table_names.roles')
) or, if configuration not found,Model::getTable()
. So ifpermission.table_names.roles
is configured, it has no change to return $this->table.I would recommend to rewrite this method like this:
or for best practice remove method Role::getTable() and replace it with
The text was updated successfully, but these errors were encountered: