jeudi 25 février 2016

How to extract an array item from an array variable in Laravel 5?

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;

Appreciate any comments or suggestions. thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire