其他分享
首页 > 其他分享> > 尝试运行种子类时发出“语法:: parameterize()必须为数组类型”的问题

尝试运行种子类时发出“语法:: parameterize()必须为数组类型”的问题

作者:互联网

我在Laravel 5.3.x(最新)中有以下播种器类:

public function run()
{
    $faker = Faker::create();
    $bands = \App\Band::all()->pluck('id')->toArray();

    for ($i = 1; $i < 100; $i++) {
        \App\Album::create([
            'band_id'          => $faker->randomElements($bands),
            'name'             => $faker->name,
            'recorded_date'    => $faker->date(),
            'release_date'     => $faker->date(),
            'number_of_tracks' => $faker->randomNumber(5),
            'label'            => $faker->sentence(3),
            'producer'         => $faker->company,
            'genre'            => $faker->randomElements([
                'Art Punk',
                'Alternative Rock',
                'College Rock',
                'Experimental Rock',
                'Folk Punk',
                'Goth / Gothic Rock',
                'Grunge',
                'Hardcore Punk'
            ], 1)
        ]);
    }
}

每次尝试运行种子时,都会出现以下错误:

[Symfony\Component\Debug\Exception\FatalThrowableError]  
Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in /data/www/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 665

这是上述错误的完整堆栈跟踪:

[2016-12-24 02:17:40] local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in /data/www/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 665 in /data/www/vendor/laravel/framework/src/Illuminate/Database/Grammar.php:118
Stack trace:
#0 /data/www/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php(665): Illuminate\Database\Grammar->parameterize('Rebeka Schimmel')
#1 /data/www/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php(683): Illuminate\Database\Query\Grammars\Grammar->compileInsert(Object(Illuminate\Database\Query\Builder), Array)
#2 /data/www/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2150): Illuminate\Database\Query\Grammars\Grammar->compileInsertGetId(Object(Illuminate\Database\Query\Builder), Array, 'id')
#3 [internal function]: Illuminate\Database\Query\Builder->insertGetId(Array, 'id')
#4 /data/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(1466): call_user_func_array(Array, Array)
#5 /data/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1605): Illuminate\Database\Eloquent\Builder->__call('insertGetId', Array)
#6 /data/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1574): Illuminate\Database\Eloquent\Model->insertAndSetId(Object(Illuminate\Database\Eloquent\Builder), Array)
#7 /data/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1471): Illuminate\Database\Eloquent\Model->performInsert(Object(Illuminate\Database\Eloquent\Builder))
#8 /data/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(561): Illuminate\Database\Eloquent\Model->save()
#9 /data/www/database/seeds/AlbumsTableSeeder.php(36): Illuminate\Database\Eloquent\Model::create(Array)
#10 /data/www/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php(63): AlbumsTableSeeder->run()
#11 /data/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(2288): Illuminate\Database\Console\Seeds\SeedCommand->Illuminate\Database\Console\Seeds\{closure}()
#12 /data/www/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php(64): Illuminate\Database\Eloquent\Model::unguarded(Object(Closure))
#13 [internal function]: Illuminate\Database\Console\Seeds\SeedCommand->fire()
#14 /data/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(508): call_user_func_array(Array, Array)
#15 /data/www/vendor/laravel/framework/src/Illuminate/Console/Command.php(169): Illuminate\Container\Container->call(Array)
#16 /data/www/vendor/symfony/console/Command/Command.php(254): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#17 /data/www/vendor/laravel/framework/src/Illuminate/Console/Command.php(155): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#18 /data/www/vendor/symfony/console/Application.php(821): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#19 /data/www/vendor/symfony/console/Application.php(187): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Database\Console\Seeds\SeedCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#20 /data/www/vendor/symfony/console/Application.php(118): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#21 /data/www/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(121): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#22 /data/www/artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#23 {main}  

为什么?

解决方法:

将$faker-randomElements()更改为$faker-> randomElement(). band_id期望一个整数并给出一个数组.流派期望一个字符串并给出一个数组.

标签:laravel,php,laravel-5-3
来源: https://codeday.me/bug/20191111/2023248.html