尝试使用laravel 5.3中的faker为数据库设定种子时的数组到字符串转换

时间:2016-11-08 15:25:43

标签: php laravel-5 faker

具体而言,此错误仅发生在此:

$faker->randomElements($array = array('long','short','shoulder length')),

php artisan db:seed

的输出
    [Illuminate\Database\QueryException]                                                                  
  Array to string conversion (SQL: insert into `profiles` (`agency_id`, `name`, `bio`, `age`, `hair_ty  
  pe`, `updated_at`, `created_at`) values (1, Cathy, Placeat voluptas tenetur corrupti et., 44, short,  
   2016-11-08 15:09:00, 2016-11-08 15:09:00)) 

     [ErrorException]            
  Array to string conversion 

是否需要添加任何属性才能使其工作,sql清楚地显示查询异常中随机生成的值,laravel日志中没有任何内容。

播种机:

namespace App;

use App\Profile;

use Illuminate\Database\Seeder;

class ProfileTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {

        $faker = \Faker\Factory::create();

        foreach(range(1,50) as $index)
        {
            Profile::create([
                'agency_id'         =>  '1',
                'name'              =>  $faker->firstName($gender = 'female'),
                'bio'               =>  $faker->sentence(4),
                'age'               =>  $faker->numberBetween($min=19, $max=46),
                'hair_type'         =>  $faker->randomElements($array = array('long','short','shoulder length'))
       ]);
    }
  }
}

1 个答案:

答案 0 :(得分:1)

@Mihailo - 谢谢,有时候你看不出来看。

有两种定义:

$faker->randomElements($array = array('a','b','c'), $count = 1) //array('c')
$faker->randomElement($array = array('a','b','c')) //b

我正在使用前者并期待后者的结果。

由于