I’m passing two object in a POST request with axios from client (js) to server (php with laravel)
orderData and userData contain mutliple value, nested array…
export const sendMail = (orderData, userData) => async () => {
await axios({
method: 'post',
url: `${process.env.REACT_APP_API_URL2}mail`,
data: { orderData: orderData, userData: userData },
}).then((res) => {
console.log('update mail send with success');
});
};
Then, in laravel, i would like to access data.
i need to access name/email in userData, and title in orderData
What i have tried :
$data = $request->all();
$orderDatas = $data['orderData'];
$UserDatas = $data['userData'];
$userName = $UserDatas->get('name');
$userEmail = $UserDatas->get('email');
$title = $orderDatas->get('title');
I also know how to access data if i only pass one object in my request (for exemple, if I only pass "userData", i know " $userName = $request->get('name');
" will get me the user name.
my error : "Call to a member function get() on array"
.
2
Answers
You don’t really need
$request->all()
, as I assume the method hasRequest $request
as part of the parameters.orderData
will be inside$request->orderData
userData
will be inside$request->userData
print_r
,var_dump
ordd
will show you the structure of$request->userData
and$request->orderData
so you can further use this data. I don’t know the structure of those, but we can pretend thename
key exists with$request->userData['name']
for example key access.ie:
var_dump($request->orderData);
A side note that the axios could be shortened to
you can not call get() on array as error describes this. Instead you should try this while using array.