我在Laravel 5.5中有以下工厂
use Faker\Generator as Faker;
$factory->define(App\Student::class, function (Faker $faker, $form) {
//$faker->addProvider(new \Faker\Provider\SIMS($faker));
$faker->seed(1234);
echo $faker->name;
});
当我运行php artisan db:seed
时,我得到的错误In Base.php line 194: Undefined offset: -57
的偏移量会有所不同,具体取决于我用于$faker->seed(1234)
的值。让这条线离开工厂会导致种子成功运行,但显然我每次都会得到不同的数据,因此需要为Faker种下。
Base.php是Faker的一部分,看起来像这样
public static function randomElements(array $array = array('a', 'b', 'c'), $count = 1, $allowDuplicates = false)
{
$allKeys = array_keys($array);
$numKeys = count($allKeys);
if (!$allowDuplicates && $numKeys < $count) {
throw new \LengthException(sprintf('Cannot get %d elements, only %d in array', $count, $numKeys));
}
$highKey = $numKeys - 1;
$keys = $elements = array();
$numElements = 0;
while ($numElements < $count) {
$num = mt_rand(0, $highKey);
if (!$allowDuplicates) {
if (isset($keys[$num])) {
continue;
}
$keys[$num] = true;
}
$elements[] = $array[$allKeys[$num]]; //THIS IS LINE 194.
$numElements++;
}
return $elements;
}
自安装Laravel以来,我没有修改它。
学生工厂正由表格工厂调用
factory(App\Form::class, $numberOfStudentsInForm)->create()->each(function ($form)
{
echo ("Seeding Students of Form $form->name\r\n");
factory(App\Student::class, 30)->create(['form_id'=>$form]);
});
学生模型看起来像这样
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Student extends Model
{
use SoftDeletes;
protected $primaryKey = "admission_number";
public $incrementing = false;
protected $keyType = "string";
public function form()
{
return $this->belongsTo('App\Form');
}
public function grades()
{
return $this->hasMany('App\Grade');
}
public function delete()
{
return ($this->grades()->delete() && parent::delete());
}
}
我做错了吗?