php 当模型扩展另一个类时,使模型可验证

k0pti3hp  于 2023-02-28  发布在  PHP
关注(0)|答案(1)|浏览(132)

我在我的应用程序中使用spatie/laravel-event-sourcing。我有Consultant模型,它扩展了Projection(抽象)类。

<?php

namespace App\Projections;
use Spatie\EventSourcing\Projections\Projection;

class Consultant extends Projection
{

我为Consultant模型做了auth guard,使用Breeze登录我得到这个错误:
Illuminate\Auth\EloquentUserProvider::validateCredentials(): Argument #1 ($user) must be of type Illuminate\Contracts\Auth\Authenticatable, App\Projections\Consultant given
由于Consultant应扩展Illuminate\Foundation\Auth\User,因此引发此错误
如何扩展两个类?ProjectionUser
https://github.com/spatie/laravel-event-sourcing/discussions/372
我知道php不支持多重继承。
问题是核心的laravel代码和包有类的类型检查,所以traits不能解决这个问题。我可以移动功能,但是一些方法仍然需要Illuminate\Contracts\Auth\AuthenticatableSpatie\EventSourcing\Projections\Projection的参数类型

oalqel3c

oalqel3c1#

感谢您的提示,唯一的解决方案是创建另一个类,并实现所有需要的数据:

<?php

namespace App\Contracts;

use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\MustVerifyEmail;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Spatie\EventSourcing\Projections\Projection;

class AuthenticableProjection extends Projection implements
    AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use Authenticatable;
    use Authorizable;
    use CanResetPassword;
    use MustVerifyEmail;
}

但不幸的是,在下一步中,我遇到了一个特定于项目的异常,修复它的唯一方法是重写身份验证逻辑。我不会这样做,相反,我将创建另一个模型,并在它们之间建立关系

Spatie \ EventSourcing \ Projections \ Exceptions \ ReadonlyProjection
The `App\Projections\Consultant` projection is not writeable at this point, please call `$model->writeable()` first.

相关问题