6

We had this webservice running in Laravel 5 and AngularJs/Ionic for handling the web. When we post request from the web (client) to webservice (backend), we passed nested JSON object. We are having an issue to read all child objects under parent object in the server side.

{
    "name": "test",
    "description": "test",
    "startdate": "2016-02-21T13:00:00.000Z",
    "enddate": "2016-02-23T13:00:00.000Z",
    "coach": {
        "uuid": "76fdd664-d830-11e5-9d46-00ffc9587cbc"
    },
    "category": {
        "uuid": "771e6de4-d830-11e5-9d46-00ffc9587cbc"
    },
    "useruuid": "76d65a2d-d830-11e5-9d46-00ffc9587cbc",
    "routines": ["775b2726-d830-11e5-9d46-00ffc9587cbc"]
}

This JSON has been verified ok and I also we managed to get the basic one such as name, endate etc etc BUT not the nested object one.

We are using something like this in Laravel 5:

$incomingdata = $request->json()->all();
$name = $incomingdata->name; // works
$startdate = $incomingdata->startdate; // works
$coach_uuid = $incomingdata->coach()->uuid; // didn't work !!!

How do I achieve this?

8
  • I'm pretty confused about what's going on here. So does the client send the JSON to the server? Commented Feb 22, 2016 at 18:36
  • @OliverQueen : Correct! Client sent JSON data with nested object to the server running on Laravel 5. Commented Feb 22, 2016 at 18:56
  • Okay, I'd definitely suggest using json_decode() Commented Feb 22, 2016 at 18:57
  • @OliverQueen : I made adjustments on my question to make it clear. Commented Feb 22, 2016 at 19:00
  • what does print_r($incomingdata); return? Commented Feb 22, 2016 at 19:01

2 Answers 2

7

I don't know about Laravel 5.0, but in Laravel 5.6 I had to do something quite different. None of the code in other answers worked for me.

Here is what I found to work correctly:

$name = $request->input('name');
$startdate = $request->input('startdate');
$coach_uuid = $request->input('coach.uuid');
print_r($name.'<br />');
print_r($startdate.'<br />');
print_r($coach_uuid.'<br />');

The above prints:

test
2016-02-21T13:00:00.000Z
76fdd664-d830-11e5-9d46-00ffc9587cbc

Reference: https://laravel.com/docs/5.6/requests#retrieving-input

Sign up to request clarification or add additional context in comments.

Comments

4

You can try this:

$phpArray = json_decode($jsonObject,true);
$name = $phpArray['name'];
$coach = $phpArray['coach'];
$coach_uuid = $phpArray['coach']['uuid'];
$category = $phpArray['category']; 
$category_uuid = $phpArray['category']['uuid]; 

2 Comments

In Laravel 5, there is a method to convert: using $request->json()->all(); and so $data = $request->json()->all(); and I can access easily via $data->name or $data->enddate BUT not the nested object $data->coach->uuid or $data->category->uuid
I have to do something like this in order to work: $coach_uuid = $incomingdata['coach']['uuid']; Thanks !!! I thought there is elegent way in Laravel to achieve this :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.