I'm a beginner and currently learning Vue js and Laravel. So I'm trying to figure out how to create a single POST request via axios then insert multiple entries into my database.
I have this array for example:
{
"comakers": [
{
"name": "Sample",
"email": "[email protected]"
},
{
"name": "Test",
"email": "[email protected]"
}
]
}
then on my laravel controller I want to do a foreach loop like this
public function update(Request $request, $id)
{
$data = $request->all();
foreach ($data as $comaker){
$nominate = new Nominate();
$nominate->loan_application_id = $id;
$nominate->comaker_name = $comaker->name;
$nominate->comaker_email = $comaker->email;
$nominate->save();
}
}
But I get an error "Trying to get property 'name' of non-object". Can someone please give me some idea on how to achieve this?
And btw I'm trying to use the update function from my controller. Basically, I want to (insert new if not existing) or (edit when theres an existing data). Am I doing the right approach here?
(UPDATE)
laravel dd or dump shows this when I try to get the value of $data = $request->all();
array:1 [
"comakers" => array:2 [
0 => array:2 [
"name" => "Sample"
"email" => "[email protected]"
]
1 => array:2 [
"name" => "Test"
"email" => "[email protected]"
]
]
]
var_dump($data)and show us the output please