【问题标题】:Laravel5 : Calling a static method on eloquent model not workingLaravel5:在 eloquent 模型上调用静态方法不起作用
【发布时间】:2015-03-24 02:42:56
【问题描述】:

我在User 模型中有一个静态方法。

namespace Tol;
...
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    ...

    public static function signup(array $data)
    {
        $user = new User([
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'username' => $data['username'],
            'type' => $data['type'],
        ]);

        $user->save();

        if ($user && $user->id) {
            $profile = new UserProfile([
                'first_name' => trim($data['first_name']),
                'last_name' => trim($data['last_name']),
                'gender' => $data['gender'],
            ]);

            $user->profile()->save($profile);

            EmailVerification::sendTo($user, 'signup');
        }

        return $user;
    }
    ...

}

我试图简单地从我的控制器调用这个方法。 像这样

$user = User::signup($input);

它会抛出这样的错误:

我不知道它为什么将它称为 Builder 类的方法。代码非常简单,在 Laravel 4 时一切正常。

请帮忙。 谢谢

【问题讨论】:

标签: php eloquent laravel-5


【解决方案1】:

您的代码应该没有问题,恐怕问题出在您的auth.php文件中,请确保

'model' => 'App\User',

在您的情况下将其设置为您的模型文件

'model' => 'Tol\User',

为了确保您调用正确的文件,您可能想尝试一下

\Tol\User::signup($array);

【讨论】:

  • 它已经是Tol\Userconfig/auth.php 文件中。而且我已经尝试过\Tol\User::signup,但也没有用。
  • 我已经在我的环境中测试了你的代码,它们运行良好。你有哪个 laravel 5 安装版本? composer dumpautoload 有帮助吗?
猜你喜欢
  • 1970-01-01
  • 2013-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-05
  • 2016-08-20
  • 1970-01-01
  • 2013-11-14
相关资源
最近更新 更多