skip to Main Content

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


  1. You don’t really need $request->all(), as I assume the method has Request $request as part of the parameters.

    orderData will be inside $request->orderData

    userData will be inside $request->userData

    print_r, var_dump or dd 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 the name 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

    axios.post(`${process.env.REACT_APP_API_URL2}mail`, {orderData, userData}).then...
    
    Login or Signup to reply.
  2. you can not call get() on array as error describes this. Instead you should try this while using array.

     $data = $request->all();
     $orderDatas = $data['orderData'];
     $UserDatas = $data['userData'];
    
     $userName = $UserDatas['name'];
     $userEmail = $UserDatas['email'];
     $title = $orderDatas['title'];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search