1. Go to this page and download the library: Download kamranahmedse/laraformer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
class User extends Eloquent
{
...
public function transform(User $user) {
return [
'public_id' => $this->amalgamate($user->id),
'name' => $user->name,
'slug' => str_slugify($user->name),
'occupation' => $user->profession,
'is_admin' => (bool) $user->is_admin,
'joined_on' => DateHelper::humanize($user->created_at),
'profile_design' => json_decode($user->design_options, true)
];
}
}
class UserController extends Controller
{
...
// Works well with model object
public function show($id) {
return User::find($id);
}
...
// Or you can return the collection
public function all() {
return User::all();
}
...
// Also paginated data is gracefully handled
public function paginate() {
return User::paginate(10);
}
}
// Use the registered alias
$user = User::find(120);
$transformedUser = Laraformer::transformModel($user);
// Do something with $transformedUser
// Transforming using callback function
return Laraformer::forceTransform($dataset, function ($item) {
return [
'name' => $item['name'],
'slug' => str_slugify($item['name']),
'occupation' => $item['profession'],
'is_admin' => (bool) $item['is_admin'],
'joined_on' => DateHelper::humanize($item['created_at']),
'profile_design' => json_decode($item['design_options'], true)
];
})
// Transforming using Transformer class object
$userTransformer = new UserTransformer;
return Laraformer::forceTransform($dataset, $userTransformer)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.