1

I'm new to laravel and now currently using Laravel version 5.2.

I have this set of array:

    {
        "id": 2,
        "title": 'sample',
        "question_index": 1,
        "question": "Hope you could help me buddy.",
        "data": [
                    {
                        "id": 1,
                        "answer": 'yes',
                    }
                ]
    }

The array was created using the following eloquent query:

$exam_data = $this->template->where('id', $id)
                            ->with('data')
                            ->get();

Now I wanted to retrieve the 'data' array inside that array using Laravel 5.

I tried the following code but to no avail...

$user_data = $exam_data->data;

$user_data = $exam_data['data'];

$user_data = $exam_data->get('data');

Though I can pluck it using this:

$user_data = $exam_data->pluck('data');

which has this result (I don't know why but I'm not satisfied on it):

[
    [
        {
            "id": 1,
            "answer": 'yes',
        }
    ]
]

Is there any ways or method that I can use to retrieve the 'data' array?

Like For example:

$user_data = $exam_data->data;

and how about accessing 'answer' field?

$user_data = $exam_data->data->answer;
Is this possible?

Appreciate any comments or suggestions. thanks!

3
  • can you try $exam_data['data']? Commented Feb 25, 2016 at 10:49
  • @emuigai Yup I already did that. I also tried to convert it to JSON and Array but still to no avail. Commented Feb 25, 2016 at 10:52
  • @AlexeyMezenin precisely yes. Commented Feb 25, 2016 at 10:54

1 Answer 1

1
$exam_data = $this->template->where('id', $id)
                            ->with('data')
                            ->get();

returns a Collection of multiple records, something like:

Collection [
  0 => Collection,
  1 => Collection,
]

That's why you can't access "data" directly from $exam_data, and that's what ->pluck() does for you.

pluck() does the same as:

$filtered = [];
foreach ($exam_data as $exam) {
  $filtered[] = $exam->data;
}

if you dd($filtered) you get what you're after.

If you are expecting a single record, though, you could use ->first() instead of ->get(), which returns a single Collection and there you can access the data relationship with:

$exam_data = ..... ->first();
$data = $exam_data->data;
Sign up to request clarification or add additional context in comments.

3 Comments

Well, it depends on the relationship, if is single or multiple (belongsTo or belongsToMany, for ex.). dump $exam_data->data and you'll see if it's a collection of multiple items or just one ;)
Wow a thorough explanation! I tried it and it works. The first() method saves the trouble. Thanks mate. :)
I tried accessing the member and the first() method also applies. It's all about recursion after all. Now I understand everything needed for it. thanks much

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.