1

My project is on angularjs+Laravel.

From frontend I am sending dynamically generated input to laravel controller.

Scenario is I am taking family details where child field is dynamic.So if you click of Add more It will generate another input field.

Initially Person X have 1 child namely ROohan . Now he Have 2 more, Now when he is sending data of 3 new children, I want that 2 children data get added without over writing present 2 children.

My request which I am receiving in controller

family_data : 
      {"famdet":[{"id":"choice1","cname":"Rohan","doa":"2017-01-12"},  
       {"id":"choice2","cname":"sohan","cdob":"2017-01-13"},           
       {"id":"choice3","cname":"nitesh","cdob":"2017-01-07"}
          ]}

my problem is How to make this data save in separate row and if data is present then row should not be get created.

1
  • Create a model and corresponding database table. Then loop through each of the inputs, check it the models already exist and if not, create them. Commented Jan 17, 2017 at 16:05

1 Answer 1

1

First you need to use json_decode() to convert data to an array, for example:

$familyData = json_decode($data, true)['famdet'];

Then you should iterate over this array and use updateOrCreate() method to update existing rows or create new ones. For example, if you're using and ID to identify a row:

foreach ($familyData as $row) {
    Model::updateOrCreate(['id' => $row['id']], $row)
}
Sign up to request clarification or add additional context in comments.

2 Comments

can you please elobrate more. I am not getting proper output. @Alexey Mezenin
How can I remove the field from table if user has removed some particular data. @AlexeyMezenin

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.