使用zizaco进行种子处理时,BadMethodCallException App \ User :: create()

时间:2017-09-01 00:50:42

标签: php laravel laravel-5

我在使用laravel 5.5播种到数据库时遇到错误,错误消息如下,并且有我的用户类和播种器类。发生的事情是在调用db:seed时正在插入一条记录但在第一次调用之后它会在下面显示BadMethodException

[BadMethodCallException]
Call to undefined method App\User::create()

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;
use Eloquent;

class User extends Eloquent
{
    use EntrustUserTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

<?php

use App\User;
use Faker\Factory as Faker;
use Illuminate\Database\Seeder;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {       
        foreach (range(1, 100) as $index) {
            $faker = Faker::create();
            $user = User::create([
                'name' => $faker->firstName . ' ' . $faker->lastName,
                'email' => $faker->email,
                'password' => bcrypt('secret')
            ]);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您的用户模型应该扩展

<% headers = ['Date', 'Min Temp', 'Max Temp'] %>
<%= CSV.generate_line headers %>
<% @results.each do |key| %>
<%= CSV.generate_line([key[0], key[1][:min_temp], key[1][:max_temp]]).html_safe %>
<% end %>

或者

\Illuminate\Database\Eloquent\Model

您正在扩展Eloquent

laravel / laravel存储库:https://github.com/laravel/laravel/blob/master/app/User.php

\Illuminate\Foundation\Auth\User

答案 1 :(得分:0)

您的用户类需要扩展为模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
     ...
}

修改

这是我的用户

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;

    protected $table = 'users';


    protected $fillable = ['name', 'email', 'password'];


    protected $hidden = ['password', 'remember_token'];


}

希望它有所帮助!