0

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',
  ),
)
Sidney Sousa
  • 53
  • 1
  • 6
  • You need to give us the error message before anyone's going to have anything helpful to provide. – miken32 Nov 05 '20 at 18:03
  • Unfortunately that is not the answer @sta . Generally if you have double quotes you are free to use variables with the dollar sign inside a string. I tried your suggestion to re-ascertain though. – Sidney Sousa Nov 05 '20 at 18:31
  • What the error you got? – STA Nov 05 '20 at 18:32
  • I get a 500 internal server first, followed by undefined in the console. – Sidney Sousa Nov 05 '20 at 18:33
  • On .env change `APP_DEBUG=true` and get the error – STA Nov 05 '20 at 18:35
  • @sta I get this: [2020-11-05 18:52:44] local.ERROR: Array to string conversion {"exception":"[object] (ErrorException(code: 0): Array to string conversion at /Users/user/Desktop/projects/laravel/adp-project4/app/Http/Controllers/ProjectsController.php:19) [stacktrace] – Sidney Sousa Nov 05 '20 at 18:53
  • What the output of `dd($fields);`? – STA Nov 05 '20 at 18:54
  • @sta dump and die will not work because the application does not have a front-end. However, I put the $fields inside a json_encode and the console printed this: (2) [{…}, {…}] 0: {_token: "hRQyJ9NlwLdKpZbU7W2REV2I5YF6Pg8D6HcoCof7", name: "dqd", type: "dqwd", default: "dwqdwq"} 1: {_token: "hRQyJ9NlwLdKpZbU7W2REV2I5YF6Pg8D6HcoCof7", name: "wdqw", type: "wdqd", default: "qdw"} length: 2 __proto__: Array(0) – Sidney Sousa Nov 05 '20 at 18:59
  • The application have no front-end, but you can see the error from the Network (as same you see the error on console). Give the output from **Network** > **Preview**, as screenshot https://datatables.net/media/images/tn/1/chrome-4.png – STA Nov 05 '20 at 19:02
  • @SidneySousa `Array to string conversion at /Users/user/Desktop/projects/laravel/adp-project4/app/Http/Controllers/ProjectsController.php:19)` what is on line 19 here – bhucho Nov 05 '20 at 19:05
  • @sta I just edited the question as I managed to log what's inside the $fields – Sidney Sousa Nov 05 '20 at 19:12
  • `"value of $key is $value"` won't work if `$value` is an array, hence the error message. – miken32 Nov 05 '20 at 19:22
  • You have an array, inside another array, so this will be `$a .= "value of $key is $value['name']";` – STA Nov 05 '20 at 19:25

1 Answers1

1

you are doing the concatenation wrong I think,

$fields = $request->fields;
$a = '';
foreach ($fields as $field){
    foreach ($field as $key => $value) {
        $a .= " value of " . $key . " is " . is_string($value) ? $value : 
        implode(', ',$value);
    }
}
return $a;
bhucho
  • 3,903
  • 3
  • 16
  • 34