Reputation: 658
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.
Upvotes: 1
Views: 872
Reputation: 163978
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)
}
Upvotes: 1