Laravel的工厂功能不起作用

时间:2015-06-16 06:00:31

标签: php laravel laravel-5

嗨,我是Laravel的新手。我正在尝试使用模型工厂为我的数据库播种,但在测试我的工厂时,工厂函数抱怨错误。

enter image description here

用户模型

class User extends Model {

    protected $table = 'users';
    use SoftDeletes;
    protected $dates = ['deleted_at'];

    protected $fillable = [
        'name',
        'email',
        'password',
        'remember_token',
        'ip_address',
        'active',
        'deleted_at',
    ];

}

工厂

$factory->define('App\User', function ($faker) {
    return [

        'name' => $faker->name,
        'email' => $faker->safeEmail,
        'password' => $faker->word,
        'remember_token' => $faker->word,
        'ip_address' => $faker->streetAddress,
        'active' => $faker->randomElement($array = array('',null)),
        'created_at' => $faker->dateTime($max = 'now'),
        'updated_at' => $faker->dateTime($max = 'now'),
        'deleted_at' => $faker->dateTime($max = 'now'),

    ];
});

我哪里出错了?

2 个答案:

答案 0 :(得分:0)

你的用户模型中有这些行吗?

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

实际上很高兴看到以<?php

开头的完整文件

答案 1 :(得分:0)

在Faker之前添加Faker

$factory->define('App\User', function (Faker $faker) {
    return [

        'name' => $faker->name,
        'email' => $faker->safeEmail,
        'password' => $faker->word,
        'remember_token' => $faker->word,
        'ip_address' => $faker->streetAddress,
        'active' => $faker->randomElement($array = array('',null)),
        'created_at' => $faker->dateTime($max = 'now'),
        'updated_at' => $faker->dateTime($max = 'now'),
        'deleted_at' => $faker->dateTime($max = 'now'),

    ];
});