duanou3868 2016-11-08 15:25
浏览 59

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

specifically this error only occurs with this:

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

Output from 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 

Is there any attributes that I need to add to make this work, the sql clearly shows the randomly generated value in the query exception, nothing in the laravel logs.

Seeder:

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条回答 默认 最新

  • dpj83664 2016-11-08 15:55
    关注

    @Mihailo - Thanks sometimes you just can't see for looking.

    There are two definitions:

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

    I was using the former and expecting the result from the latter.

    Thanks

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line