eloquent - How do I make traits share conflicting methods and properties in PHP - Stack Overflow

I have this traitHasExtendedRoles.phptrait HasExtendedRoles{use HasRoles;use HasExtendedPermissions

I have this trait

HasExtendedRoles.php

trait HasExtendedRoles
{
    use HasRoles;
    use HasExtendedPermissions {
        HasExtendedPermissions::getPermissionClass insteadof HasRoles;
        HasExtendedPermissions::scopePermission insteadof HasRoles;
        HasExtendedPermissions::permissions insteadof HasRoles;
    }

    protected ?Form $currentForm = null;

    public function forForm(string|int|Form $form): self
    {
        if (!$form instanceof Form) {
            $form = Form::when(
                is_numeric($form),
                fn(Builder $q) => $q->where('id', $form),
                fn(Builder $q) => $q->where('slug', $form)
            )->firstOrFail();
        }

        $this->currentForm = $form;

        return $this;
    }
}

I also have this trait

HasExtendedPermissions.php

trait HasExtendedPermissions
{
    use HasPermissions;

    protected ?Form $currentForm = null;

    public function forForm(string|int|Form $form): self
    {
        if (!$form instanceof Forsm) {
            $form = Form::when(
                is_numeric($form),
                fn(Builder $q) => $q->where('id', $form),
                fn(Builder $q) => $q->where('slug', $form)
            )->firstOrFail();
        }

        $this->currentForm = $form;

        return $this;
    }
}

And in my User model

class User extends Authenticatable
{
    use HasExtendedRoles;
}

And in my controller, I do

dd($user->forForm($form)->permissions);

Now the HasExtendedPermissions trait doesn't seam to inherit $this->currentForm.

But when I do:

dd($user->forForm($form)->roles);

Which is available through the HasExtendedRoles trait, it works perfectly, I was thinking about creating another forForm method inside the HasExtendedPermissions trait with a diffrent name but I hope to minimize code duplication as much as possible.

What could I possibly be doing wrongly?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744870170a4598212.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信