最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论