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?

afuzzyllama
  • 6,538
  • 5
  • 47
  • 64

2 Answers2

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