当我尝试将数据保存到数据库中时,出于某种原因我只能保存1个整数而不是多个整数。谁能告诉我为什么?我当时认为这可能是因为我在数据库中使用FamilyAge作为整数而不是字符串。
以下是我数据库中数据的屏幕截图,对不起我输入的奇怪数据,我只是输入一些随机单词进行测试。 正如您在我的截图中看到的那样,最后一行,我想要保存3个数据,但对于家庭年龄部分,只保存1个数据。
family_info控制器(这就是我保存它的方式)
public function submit(Request $request)
{
$personal_info = Session::get('data');
$data7 = array();
$data7['NameOfFamily'] = implode(' , ', $request->NameOfFamily);
$data7['Relationship'] = implode(' , ', $request->Relationship);
$data7['FamilyAge'] = implode(' , ', $request->FamilyAge);
$family_info = new family_info;
$family_info = family_info::create($data7);
$family_info->personal_infos()->associate($personal_info);
$family_info->save();
}
familyInfos表:
Schema::create('family_infos', function (Blueprint $table) {
$table->increments('id');
$table->engine = 'InnoDB';
$table->string('NameOfFamily');
$table->string('Relationship');
$table->integer('FamilyAge');
$table->integer('user_id');
$table->timestamps();
});
答案 0 :(得分:1)
通过查看您的架构,您使用的是FamilyAge $table->integer('FamilyAge');
的整数类型,它应该是一个用于存储逗号分隔值的字符串$table->string('FamilyAge');
我想你正试图像这样22, 45, 56
Schema::create('family_infos', function (Blueprint $table) {
$table->increments('id');
$table->engine = 'InnoDB';
$table->string('NameOfFamily');
$table->string('Relationship');
$table->string('FamilyAge');
$table->integer('user_id');
$table->timestamps();
});
我建议你阅读这篇文章 https://laravel.com/docs/5.5/migrations#columns解释了如何做到 创建Laravel迁移