Skip to content

Instantly share code, notes, and snippets.

@rizqidjamaluddin
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rizqidjamaluddin/fb190820f478a432c591 to your computer and use it in GitHub Desktop.
Save rizqidjamaluddin/fb190820f478a432c591 to your computer and use it in GitHub Desktop.
Dead simple transformers
<?php
class UserController {
public function getUserForApi($id) {
return (new UserTransformer())->transform(User::find($id));
}
}
<?php
class UserTransformer {
public function transform(User $user) {
return [
'name' => $user->name,
'email' => $user->email,
'join_date' => $user->created_at->format("Y-m-d H:i:s"),
'profile_url' => route("user-profile", ['id' => $user->id])
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment