Skip to content
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

Call to undefined method Spatie\Permission\Models\Permission::translationEnabled() #306

Closed
NARTONIC opened this issue Jul 8, 2022 · 11 comments
Assignees
Labels

Comments

@NARTONIC
Copy link

NARTONIC commented Jul 8, 2022

Bug report

After create a Role, when I want to edit, returns this error
Call to undefined method Spatie\Permission\Models\Permission::translationEnabled()

What I did:

Follows the guide

What I expected to happen:

Edit the role I've created

What happened:

Returns error Call to undefined method Spatie\Permission\Models\Permission::translationEnabled()

What I've already tried to fix it:

Backpack, Laravel, PHP, DB version:

Back Pack latests version 5.3
PHP 8.1

@welcome
Copy link

welcome bot commented Jul 8, 2022

Hello there! Thanks for opening your first issue on this repo!

Just a heads-up: Here at Backpack we use Github Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.

Backpack communication mediums:

  • Bug Reports, Feature Requests - Github Issues (here);
  • Quick help (How do I do X) - Gitter Chatroom;
  • Long questions (I have done X and Y and it won't do Z wtf) - Stackoverflow, using the backpack-for-laravel tag;

Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.

Thank you!

--
Justin Case
The Backpack Robot

@kasperbjerby
Copy link

kasperbjerby commented Jul 8, 2022

Can confirm this bug

PHP 8.1.8
Laravel 9.19.0
Backpack 5.1.4

@CamusMX7
Copy link

CamusMX7 commented Jul 9, 2022

The permissions and roles worked perfectly but in some update they stopped working.
II try to edit a role and get error: Call to undefined method Spatie\Permission\Models\Permission::translationEnabled()
Not in all roles, only in some cases.

PHP 8.1.2
Laravel 9.1.0
Backpack 5.1.3

@kiddtang
Copy link
Contributor

kiddtang commented Jul 10, 2022

I try to troubleshoot... The culprit is the backpack/crud v5.1.3

✔️ - Upgrading spatie/laravel-permission (5.5.4 => 5.5.5)
✔️ - Upgrading backpack/crud (5.1.0 => 5.1.1)
✔️ - Upgrading backpack/crud (5.1.1 => 5.1.2)
❌ - Upgrading backpack/crud (5.1.2 => 5.1.3)

After backpack/crud is upgraded to 5.1.3, Role unable to edit and receive the error

BadMethodCallException
Call to undefined method Spatie\Permission\Models\Permission::translationEnabled()

Upgrade to the latest 5.1.4 doesn't help. temporary downgrade to 5.1.2

composer update backpack/crud:5.1.2

Commented out setupRelatedModelLocale in src/app/Library/CrudPanel/Traits/Update.php then the issue disappeared.
It is introduced in backpack/crud v5.1.3
PR - Related models should have the main entry locale too #4359

@pxpm
Copy link
Contributor

pxpm commented Jul 11, 2022

Hey @kiddtang @CamusMX7 @NARTONIC and @kasperbjerby

I am trying to work out this issue .. The problem is that the model you guys are using does not use the CrudTrait.
The function translationEnabled() is a CrudTrait method... So I think we have two ways to go here:
1 - Add the CrudTrait to the model (since you are using Crud functions, the models you use should have the CrudTrait)
2 - Add pre-check to check if model has the crud trait before checking if the translations are enabled .

Let me talk with @tabacitu to try to figure out the best way to solve this,
It will be fixed later today when we tag a new version, one way or another.

Thanks for the report and sorry for the bad experience.

Cheers

@tabacitu
Copy link
Member

Wow such a good reply thank you @kiddtang !

Quick fix

@pxpm what if we also check that translationEnable() exists on the model? Would this fix it?

-if ($model->translationEnabled()) {
+if (method_exists($model, 'translationEnabled') && $model->translationEnabled()) {

Root cause analysis

The problem is interesting though. Like Pedro said... we probably should even get to this point.

Why does this even try to create/update on the Spatie\Permission\Models\Permission model? It should be trying to do that on Backpack\PermissionManager\app\Models\Permission::class, since:

  • that's what's configured in config/backpack/permissionmanager.php
  • that's what's used in PermissionCrudController

@tabacitu
Copy link
Member

tabacitu commented Jul 11, 2022

Wait wait wait! I think I understand now! 🤯 This error appears when you:

  • try to edit a Role (which will be Backpack\PermissionManager\app\Models\Role::class
  • but the form will also have Permissions (!), which thanks to the model relationship will take you to Spatie\Permission\Models\Permission, not Backpack\PermissionManager\app\Models\Permission (which is also ✅);

The problem is in CRUD - that it now requires all related models to have CrudTrait - which is not ok ❌

In this case... I think the quick-fix I suggested above will also be a good long-term fix.
Can you double-check my findings @pxpm ?

@pxpm
Copy link
Contributor

pxpm commented Jul 11, 2022

@tabacitu I didn't get so deep on why the model is backpack or spatie because you can manually change those and use whatever you want as model, but you are right 💯 on the fact that not using the CrudTrait in ANY related model will trigger the error, and that is the real problem. So I also 💯 agree with the solution you proposed, similar to what I suggedted in 2. We can check if method exists or for CrudTrait presence (since the method is there).

I will submit a PR with with your suggestion then.

Cheers

@kiddtang
Copy link
Contributor

kiddtang commented Jul 11, 2022

Just FYI. I have done many customizations, so I extend the Spatie\Permission\Models\Role

in my config/backpack/permissionmanager.php

'models' => [
        'user'       => \App\Models\User::class,
        'permission' => \App\Models\Permission::class,
        'role'       => \App\Models\Role::class,
    ],

Also, app/Models/Role.php

use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Spatie\Permission\Models\Role as OriginalRole;

class Role extends OriginalRole
{
    use CrudTrait;
...

Also, app/Models/Permission.php

use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Spatie\Permission\Models\Permission as OriginalPermission;

class Permission extends OriginalPermission
{
    use CrudTrait;
...

So, thx @pxpm @tabacitu comments...

I think I should fix this issue by swapping the models defined in the
config/permission.php

'models' => [
        'permission' => \App\Models\Permission::class,
        'role' => \App\Models\Role::class,
    ],

@pxpm
Copy link
Contributor

pxpm commented Jul 11, 2022

Nope @kiddtang , this one is on our side :) unfortunately it slipped me, I have already sent a PR to fix it.

Cheers

@tabacitu
Copy link
Member

Fixed in Laravel-Backpack/CRUD#4514

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants