使模型在扩展另一个类时可验证。

huangapple go评论122阅读模式
英文:

Make model authenticable when it is extending another class

问题

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

<?php

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

class Consultant extends Projection
{

我为Consultant模型创建了身份验证保卫,并且在使用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代码和包都有类的类型检查,所以特性无法解决这个问题。我可以移动功能,但是某些方法仍然期望Illuminate.Contracts.Auth.AuthenticatableSpatie.EventSourcing.Projections.Projection的参数类型。

英文:

I'm using spatie/laravel-event-sourcing in my app. I have Consultant model which extending Projection (abstract) class.

&lt;?php

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

class Consultant extends Projection
{

I made auth guard for Consultant model and using breeze login I getting this error:


Illuminate\Auth\EloquentUserProvider::validateCredentials(): Argument #1 ($user) must be of type Illuminate\Contracts\Auth\Authenticatable, App\Projections\Consultant given

It is thrown because Consultant should extend Illuminate\Foundation\Auth\User

How I can extend two classes?
Projection and User

https://github.com/spatie/laravel-event-sourcing/discussions/372

I know that php doesn't support multiple inheritance.

Problem is that core laravel code and package have type check of classes, so traits can't solve this issue. I can move functionality, but some methods still expects argument type of Illuminate\Contracts\Auth\Authenticatable or Spatie\EventSourcing\Projections\Projection

答案1

得分: 1

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

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
在这一点上,`App\Projections\Consultant` 投影不可写,请首先调用 `$model->writeable()`
英文:

Thank you for tips,
the only solution was create another class, and implement all needed data:

&lt;?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;
}

but unfortunately in next step I'm getting project specific exception and the only way to fix it is to rewrite authentication logic. I won't do that, instead of it I will create another model and make relationship between them

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

huangapple
  • 本文由 发表于 2023年2月27日 15:56:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定