【问题标题】:Declaration of App\Models\User::isNot(App\Models\User $user)App\Models\User::isNot(App\Models\User $user) 的声明
【发布时间】:2019-09-25 04:21:08
【问题描述】:

我收到了这个错误

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;
   }

【问题讨论】:

    标签: php laravel voyager


    【解决方案1】:

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

    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;
    }
    

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

    【讨论】:

    • 非常感谢兄弟
    猜你喜欢
    • 2017-05-14
    • 1970-01-01
    • 2021-12-28
    • 2017-06-07
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    • 2021-06-21
    相关资源
    最近更新 更多