Laravel Serialization: Serializing Models & Collections

Serializing To Arrays

$user = App\User::with('roles')->first();

return $user->toArray();
$users = App\User::all();

return $users->toArray();

Serializing To JSON

$user = App\User::find(1);

return $user->toJson();

return $user->toJson(JSON_PRETTY_PRINT);
$user = App\User::find(1);

return (string) $user;
Route::get('users', function () {
    return App\User::all();
});

Laravel Serialization: Serializing Models & Collections — Structure map

Clickable & Draggable!

Laravel Serialization: Serializing Models & Collections — Related pages: