1

i have json array from return $data->group, here the result:

[{
"id": 2,
"name": "Grup 1",
"created_at": "2016-03-16 02:09:15",
"updated_at": "2016-03-16 02:09:15",
"pivot": {
    "cabang_id": 28,
    "group_id": 2
}}]

i want to "id" and "name" from this data.

i have tried using $data->group->id and $data->group->name but error Undefined property: Illuminate\Database\Eloquent\Collection::$id

how can i take from $data->group?

1
  • Could you please provide what you've done so far? Your question is rather hard to answer as it contains very little detail. Commented Mar 15, 2016 at 19:34

2 Answers 2

1

If you consider the error:

Undefined property: Illuminate\Database\Eloquent\Collection::$id

Then it shows that you're dealing with a Collection object. You can access a Collection's attribute's using the get('key') method as user Alexy Mezenin mentioned.

So you would do something like this in your case:

$data->group->get('id')

Check the docs/api for further details:

Docs: https://laravel.com/docs/5.2/collections#method-get

Api: https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Collection.html#method_get

Normally you can make use of an Eloquent model's magic methods to access its attributes directly by their key name as you were trying to do in the code sample you gave, i.e. $data->group->id. I believe this isn't working for you because you've already returned the data as json, if you would access the object before returning it as json then you would probably be able to access the attributes using the magic methods.

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

Comments

0

Try to use ->get('id') on the object with json data. Or $request->input('id') where $request is your Request object.

Comments

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.