When I submit the button through ajax(axios), I manage to send the following data to Laravel as far as the console prints:
{name: "task", fields: Array(2)}
fields: Array(2)
0: {_token: "hRQyJ9NlwLdKpZbU7W2REV2I5YF6Pg8D6HcoCof7", name: "name1", type: "type1", default: "default1"}
1: {_token: "hRQyJ9NlwLdKpZbU7W2REV2I5YF6Pg8D6HcoCof7", name: "name2", type: "type2", default: "default2"}
length: 2
__proto__: Array(0)
name: "task"
__proto__: Object
So now I want to read the data coming from the fields array, and for each iteration, I want to add the key and the value into a string and put a new line.
So I have this in my controller:
$fields = $request->fields;
$a = '';
foreach ($fields as $key => $value) {
$a .= "value of $key is $value";
}
return $a;
When I hit the save button I get 500 internal server error.
In the process, I double checked that $fields is not empty by returning the first element and I get a success:
return $fields[0];
Below is what I have in the JS:
let crud_object = {
name: crud_name.value,
fields: data
}
console.log(crud_object)
axios.post('/dashboard/project/new', crud_object)
.then( res => console.log(res.data))
.catch(err => console.log(err.response.dta))
Aside from the object printed in the console, I see this as well:
VM73400:1 POST http://127.0.0.1:8000/dashboard/project/new 500 (Internal Server Error)
(anonymous) @ VM73400:1
dispatchXhrRequest @ app.js:285
xhrAdapter @ app.js:119
dispatchRequest @ app.js:765
Promise.then (async)
request @ app.js:542
Axios.<computed> @ app.js:567
wrap @ app.js:1131
(anonymous) @ all.js:68
What is wrong about my for each loop that am not able to retrieve the key value pairs?
EDITED: I see this inside laravel.log:
[2020-11-05 19:11:09] local.INFO: array (
0 =>
array (
'_token' => 'hRQyJ9NlwLdKpZbU7W2REV2I5YF6Pg8D6HcoCof7',
'name' => 'fewf',
'type' => 'ewfe',
'default' => 'fwefwe',
),
1 =>
array (
'_token' => 'hRQyJ9NlwLdKpZbU7W2REV2I5YF6Pg8D6HcoCof7',
'name' => 'fwef',
'type' => 'fwef',
'default' => 'wefwe',
),
)