一对多关系将整数保存到数据库laravel中

时间:2017-11-06 02:58:32

标签: laravel one-to-many

我在使用一对多关系保存FamilyAge时遇到了一些麻烦。正如你在最后一行所看到的那样,我试图保存3个不同的人,但是对于家庭年龄,只有1个值出现而不是3个。请指出错误,提前感谢并且对于我保存的奇怪单词感到抱歉在数据库中,我只是随机输入

enter image description here

Personal_info有许多家庭family_info(一对多关系)

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();

}

personal_info controller:

public function submit(Request $request)
{
  $data = array();
  $data[Name] = $request->Name;
  $data['Email'] = $request->Email;
  $personal_info = personal_info::create($data);
  $personal_info->push();
  Session::put('data', $personal_info);
}

personal_info模特:

class personal_info extends Eloquent
{
  protected $fillable = array('Name', 'Email');
  protected $table = 'personal_infos';
  protected $primaryKey = 'id';
  public function family-infos() {
  return $this->hasMany('App\family_info,'user_id');
 }

家庭信息模型

protected $fillable =     
  array('NameOfFamily','Relationship','FamilyAge','user_id');

public function personal_infos() {
    return $this->belongsTo('App\personal_info', 'user_id', 'id');
}

0 个答案:

没有答案