0

I'm using Laravel 4 Response::json function and having trouble with passing an object. I have an object of User, containing some other objects - Account and Settings. I tried to cast the object to array, but the internal objects (Account and Settings) stay as object. Is there an easy way to convert a nested object to array so I can pass it on to Response::json?

1
  • Can you post some code and a dump of the variable? Commented Nov 28, 2013 at 18:05

2 Answers 2

0

Use

(array)$object

to convert the object to an array. However, unless the class provides a specialized method for this, it will only return the public properties.

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

Comments

0

Laravel converts Objects automatically when using Response::json. So usually you wouldn't have to bother:

Response::json($user);

In case you really need an array, you can do this:

$user->toArray();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.