App\Models\User::isNot(App\Models\User $user) 声明

Declaration of App\Models\User::isNot(App\Models\User $user)

我遇到了这个错误

Declaration of App\Models\User::isNot(App\Models\User $user) should be compatible with Illuminate\Database\Eloquent\Model::isNot($model)

对于我的代码,我在 laravel 中使用 voyager 包知道我该如何解决它

<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends \TCG\Voyager\Models\User
 {


  public function isNot(User $user){
       return $this->id !== $user->id;
   }

通过定义类型提示,与您覆盖的原始代码相比,您正在更改方法签名。

参见Eloquent/Model

/**
 * Determine if two models are not the same.
 *
 * @param  \Illuminate\Database\Eloquent\Model|null  $model
 * @return bool
 */
public function isNot($model)
{
    return ! $this->is($model);
}

换句话说,您可能想要这样的东西:

public function isNot($user) {
    return $this->id !== $user->id;
}

或者可能:

public function isNot($user) {
    if (!$user instanceof User) {
        throw new \InvalidArgumentException('Expected an instance of User');
    }
    return $this->id !== $user->id;
}

这个解决方案并不理想,但它确保您保持原始方法签名。