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

时间:2019-05-07 13:25:30

标签: php laravel voyager

我遇到此错误

  

App \ Models \ User :: isNot(App \ Models \ User $ user)的声明应为   与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;
   }

1 个答案:

答案 0 :(得分:4)

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

请参见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;
}

此解决方案并不理想,但可以确保您维护原始方法签名。