0

when I try to save my data into the database, for some reason I can only save 1 integer and not multiple integer inside. Can anyone tell me why? I was thinking it might be because I am using FamilyAge as an integer in the database instead of a string.

Here is the screenshot of the data in my database, sorry for the weird data I put, I just type some random words for testing. As you can see in my screenshot, last row, I am suppose to save 3 datas but for the family age part only 1 data is saved. enter image description here

family_info controller(this is how I saved it)

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 table:

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();
});
2
  • Your data type should be varchar to store strings. As you are adding comma separated age. Commented Nov 7, 2017 at 3:17
  • @Suraj thanks for replying, what do you mean by my data type must be varchar? How do I do that? I only know in the database I can set it as varchar but how do I do it in code. Could you show me an example? Commented Nov 7, 2017 at 3:32

1 Answer 1

1

By looking at your schema you are using an integer type for FamilyAge $table->integer('FamilyAge'); which should be a string to store comma separated values $table->string('FamilyAge');

I suppose you are trying to save the family age like this 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();
});

I recommend you to read this https://laravel.com/docs/5.5/migrations#columns which explains how to create the Laravel migration

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.