ErrorException:数组到字符串的转换。尝试在 hasMany 关系上创建 Many() 时
ErrorException: Array to string conversion. When trying to createMany() on a hasMany relationship
我一直在胡思乱想,似乎无法弄清楚。我为他们的类别抓取了 foursquare,并试图使用它们在我的数据库中播种类别。我正在使用 PHP 和 Laravel.
这是 CategorySeeder 的代码:
class CategorySeeder extends Seeder
{
public function run()
{
$categories = json_decode(file_get_contents('foursquare_categories.json', true), true);
foreach ($categories as $category)
{
$this->createCategory($category);
}
}
private function createCategory($categoryData)
{
try
{
DB::beginTransaction();
$category = Category::where('name', $categoryData['name'])->first();
if (!$category)
{
$category = new Category();
$category->fill([
'name' => $categoryData['name']
]);
$category->save();
}
$category->childCategories()->createMany($categoryData['sub_categories']);
DB::commit();
}
catch (Exception $e)
{
DB::rollBack();
}
if (count($categoryData['sub_categories']) > 0)
{
foreach ($categoryData['sub_categories'] as $subCategory)
{
if (count($subCategory['sub_categories']) > 0)
{
$this->createCategory($subCategory);
}
}
}
}
}
这是堆栈跟踪日志:
Stack trace:
#0 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(231): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'Array to string...', '/var/www/html/v...', 674)
#1 /var/www/html/vendor/laravel/framework/src/Illuminate/Support/Str.php(674): Illuminate\Foundation\Bootstrap\HandleExceptions->Illuminate\Foundation\Bootstrap\{closure}(2, 'Array to string...', '/var/www/html/v...', 674)
#2 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/QueryException.php(57): Illuminate\Support\Str::replaceArray('?', Array, 'insert into `ca...')
#3 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/QueryException.php(40): Illuminate\Database\QueryException->formatMessage('insert into `ca...', Array, Object(PDOException))
#4 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php(717): Illuminate\Database\QueryException->__construct('insert into `ca...', Array, Object(PDOException))
#5 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php(676): Illuminate\Database\Connection->runQueryCallback('insert into `ca...', Array, Object(Closure))
#6 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php(502): Illuminate\Database\Connection->run('insert into `ca...', Array, Object(Closure))
#7 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php(454): Illuminate\Database\Connection->statement('insert into `ca...', Array)
#8 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/Processor.php(32): Illuminate\Database\Connection->insert('insert into `ca...', Array)
#9 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(3170): Illuminate\Database\Query\Processors\Processor->processInsertGetId(Object(Illuminate\Database\Query\Builder), 'insert into `ca...', Array, 'id')
#10 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(1707): Illuminate\Database\Query\Builder->insertGetId(Array, 'id')
#11 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1190): Illuminate\Database\Eloquent\Builder->__call('insertGetId', Array)
#12 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1155): Illuminate\Database\Eloquent\Model->insertAndSetId(Object(Illuminate\Database\Eloquent\Builder), Array)
#13 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(996): Illuminate\Database\Eloquent\Model->performInsert(Object(Illuminate\Database\Eloquent\Builder))
#14 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php(309): Illuminate\Database\Eloquent\Model->save()
#15 /var/www/html/vendor/laravel/framework/src/Illuminate/Support/helpers.php(302): Illuminate\Database\Eloquent\Relations\HasOneOrMany->Illuminate\Database\Eloquent\Relations\{closure}(Object(App\Models\Category))
#16 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php(310): tap(Object(App\Models\Category), Object(Closure))
#17 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php(324): Illuminate\Database\Eloquent\Relations\HasOneOrMany->create(Array)
#18 /var/www/html/database/seeders/CategorySeeder.php(47): Illuminate\Database\Eloquent\Relations\HasOneOrMany->createMany(Array)
#19 /var/www/html/database/seeders/CategorySeeder.php(68): Database\Seeders\CategorySeeder->createCategory(Array)
#20 /var/www/html/database/seeders/CategorySeeder.php(68): Database\Seeders\CategorySeeder->createCategory(Array)
#21 /var/www/html/database/seeders/CategorySeeder.php(25): Database\Seeders\CategorySeeder->createCategory(Array)
#22 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Database\Seeders\CategorySeeder->run()
#23 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#24 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#25 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#26 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(653): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#27 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Seeder.php(175): Illuminate\Container\Container->call(Array, Array)
#28 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Seeder.php(184): Illuminate\Database\Seeder->Illuminate\Database\{closure}()
#29 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php(75): Illuminate\Database\Seeder->__invoke()
#30 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php(155): Illuminate\Database\Console\Seeds\SeedCommand->Illuminate\Database\Console\Seeds\{closure}()
#31 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php(76): Illuminate\Database\Eloquent\Model::unguarded(Object(Closure))
#32 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\Database\Console\Seeds\SeedCommand->handle()
#33 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#34 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#35 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#36 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(653): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#37 /var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php(136): Illuminate\Container\Container->call(Array)
#38 /var/www/html/vendor/symfony/console/Command/Command.php(291): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#39 /var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php(121): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#40 /var/www/html/vendor/symfony/console/Application.php(989): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#41 /var/www/html/vendor/symfony/console/Application.php(299): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Database\Console\Seeds\SeedCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#42 /var/www/html/vendor/symfony/console/Application.php(171): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#43 /var/www/html/vendor/laravel/framework/src/Illuminate/Console/Application.php(102): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#44 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(129): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#45 /var/www/html/artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#46 {main}
它是在尝试 运行 createMany()
时发生的,我知道很多,我只是不确定为什么会失败。
最后,这是我正在阅读的json文件(如果您需要分类,请随意复制并使用它):
我解决了
所以我把$category->childCategories()->createMany($categoryData['sub_categories']);
换成了这个
foreach ($categoryData['sub_categories'] as $subCategory)
{
$subCategory['parent_id'] = $category->id;
unset($subCategory['sub_categories']);
Category::create($subCategory);
}
导致错误的原因显然是传递了 sub_category 的 'sub_categories'。我仍然不清楚为什么我没有 sub_categories 作为类别模型上的可填写列。
我一直在胡思乱想,似乎无法弄清楚。我为他们的类别抓取了 foursquare,并试图使用它们在我的数据库中播种类别。我正在使用 PHP 和 Laravel.
这是 CategorySeeder 的代码:
class CategorySeeder extends Seeder
{
public function run()
{
$categories = json_decode(file_get_contents('foursquare_categories.json', true), true);
foreach ($categories as $category)
{
$this->createCategory($category);
}
}
private function createCategory($categoryData)
{
try
{
DB::beginTransaction();
$category = Category::where('name', $categoryData['name'])->first();
if (!$category)
{
$category = new Category();
$category->fill([
'name' => $categoryData['name']
]);
$category->save();
}
$category->childCategories()->createMany($categoryData['sub_categories']);
DB::commit();
}
catch (Exception $e)
{
DB::rollBack();
}
if (count($categoryData['sub_categories']) > 0)
{
foreach ($categoryData['sub_categories'] as $subCategory)
{
if (count($subCategory['sub_categories']) > 0)
{
$this->createCategory($subCategory);
}
}
}
}
}
这是堆栈跟踪日志:
Stack trace:
#0 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(231): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'Array to string...', '/var/www/html/v...', 674)
#1 /var/www/html/vendor/laravel/framework/src/Illuminate/Support/Str.php(674): Illuminate\Foundation\Bootstrap\HandleExceptions->Illuminate\Foundation\Bootstrap\{closure}(2, 'Array to string...', '/var/www/html/v...', 674)
#2 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/QueryException.php(57): Illuminate\Support\Str::replaceArray('?', Array, 'insert into `ca...')
#3 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/QueryException.php(40): Illuminate\Database\QueryException->formatMessage('insert into `ca...', Array, Object(PDOException))
#4 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php(717): Illuminate\Database\QueryException->__construct('insert into `ca...', Array, Object(PDOException))
#5 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php(676): Illuminate\Database\Connection->runQueryCallback('insert into `ca...', Array, Object(Closure))
#6 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php(502): Illuminate\Database\Connection->run('insert into `ca...', Array, Object(Closure))
#7 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php(454): Illuminate\Database\Connection->statement('insert into `ca...', Array)
#8 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/Processor.php(32): Illuminate\Database\Connection->insert('insert into `ca...', Array)
#9 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(3170): Illuminate\Database\Query\Processors\Processor->processInsertGetId(Object(Illuminate\Database\Query\Builder), 'insert into `ca...', Array, 'id')
#10 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(1707): Illuminate\Database\Query\Builder->insertGetId(Array, 'id')
#11 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1190): Illuminate\Database\Eloquent\Builder->__call('insertGetId', Array)
#12 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1155): Illuminate\Database\Eloquent\Model->insertAndSetId(Object(Illuminate\Database\Eloquent\Builder), Array)
#13 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(996): Illuminate\Database\Eloquent\Model->performInsert(Object(Illuminate\Database\Eloquent\Builder))
#14 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php(309): Illuminate\Database\Eloquent\Model->save()
#15 /var/www/html/vendor/laravel/framework/src/Illuminate/Support/helpers.php(302): Illuminate\Database\Eloquent\Relations\HasOneOrMany->Illuminate\Database\Eloquent\Relations\{closure}(Object(App\Models\Category))
#16 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php(310): tap(Object(App\Models\Category), Object(Closure))
#17 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php(324): Illuminate\Database\Eloquent\Relations\HasOneOrMany->create(Array)
#18 /var/www/html/database/seeders/CategorySeeder.php(47): Illuminate\Database\Eloquent\Relations\HasOneOrMany->createMany(Array)
#19 /var/www/html/database/seeders/CategorySeeder.php(68): Database\Seeders\CategorySeeder->createCategory(Array)
#20 /var/www/html/database/seeders/CategorySeeder.php(68): Database\Seeders\CategorySeeder->createCategory(Array)
#21 /var/www/html/database/seeders/CategorySeeder.php(25): Database\Seeders\CategorySeeder->createCategory(Array)
#22 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Database\Seeders\CategorySeeder->run()
#23 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#24 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#25 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#26 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(653): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#27 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Seeder.php(175): Illuminate\Container\Container->call(Array, Array)
#28 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Seeder.php(184): Illuminate\Database\Seeder->Illuminate\Database\{closure}()
#29 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php(75): Illuminate\Database\Seeder->__invoke()
#30 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php(155): Illuminate\Database\Console\Seeds\SeedCommand->Illuminate\Database\Console\Seeds\{closure}()
#31 /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php(76): Illuminate\Database\Eloquent\Model::unguarded(Object(Closure))
#32 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\Database\Console\Seeds\SeedCommand->handle()
#33 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#34 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#35 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#36 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(653): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#37 /var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php(136): Illuminate\Container\Container->call(Array)
#38 /var/www/html/vendor/symfony/console/Command/Command.php(291): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#39 /var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php(121): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#40 /var/www/html/vendor/symfony/console/Application.php(989): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#41 /var/www/html/vendor/symfony/console/Application.php(299): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Database\Console\Seeds\SeedCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#42 /var/www/html/vendor/symfony/console/Application.php(171): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#43 /var/www/html/vendor/laravel/framework/src/Illuminate/Console/Application.php(102): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#44 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(129): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#45 /var/www/html/artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#46 {main}
它是在尝试 运行 createMany()
时发生的,我知道很多,我只是不确定为什么会失败。
最后,这是我正在阅读的json文件(如果您需要分类,请随意复制并使用它):
我解决了
所以我把$category->childCategories()->createMany($categoryData['sub_categories']);
换成了这个
foreach ($categoryData['sub_categories'] as $subCategory)
{
$subCategory['parent_id'] = $category->id;
unset($subCategory['sub_categories']);
Category::create($subCategory);
}
导致错误的原因显然是传递了 sub_category 的 'sub_categories'。我仍然不清楚为什么我没有 sub_categories 作为类别模型上的可填写列。