为什么我得到BadMethodCallException?

时间:2016-03-16 08:48:09

标签: laravel laravel-5.2

在我的播种器类中,我正在调用一个在User模型中定义的方法:

像这样:

$user = User::where('email', 'user1@teams.com')->get();
$user->test();

我的用户模型:

    class User extends Authenticatable
    {

        public function test()
        {
            return "!!";
        }
    }

但是,当运行种子时,我收到此错误:

  [BadMethodCallException]
  Method test does not exist.

1 个答案:

答案 0 :(得分:1)

$ user包含一组用户,因为您使用的是get()。您可以使用first()代替。 所以新代码应该是:

$user = User::where('email', 'user1@teams.com')->first();