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?
Asked
Active
Viewed 1,155 times
0
-
Can you post some code and a dump of the variable? – afuzzyllama Nov 28 '13 at 18:05
2 Answers
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.

Barmar
- 741,623
- 53
- 500
- 612
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();

Ronald Hulshof
- 1,986
- 16
- 22