PHP code example of itsdamien / laravel-model-transformer

1. Go to this page and download the library: Download itsdamien/laravel-model-transformer 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/ */

    

itsdamien / laravel-model-transformer example snippets


class UserTransformer extends \ItsDamien\Transformer\AbstractTransformer
{
    public function model($model)
    {
        return [
            'first_name' => $model->first_name,
            'last_name'  => $model->last_name,
            'full_name'  => $model->first_name.' '.$model->last_name,
            'photos'     => PhotoTransformer::transform($model->photos),
        ];
    }
}

return response([
    "user" => UserTransformer::transform(User::find(1))
]);

// Output:
// {
//     "user":{
//         "first_name":"John",
//         "last_name":"Doe",
//         "full_name":"John Doe",
//         "photos":[]
//     }
// }

return response([
    "users" => UserTransformer::transform(User::all())
]);

// Output:
// {
//     "users":[
//         {
//             "first_name":"John",
//             "last_name":"Doe",
//             "full_name":"John Doe",
//             "photos":[]
//         },
//         {
//             "first_name":"Dolores",
//             "last_name":"Abernathy",
//             "full_name":"Dolores Abernathy",
//             "photos":[]
//         },
//     ]
// }

UserTransformer::transform($user, ['foo' => 'bar']);

class UserTransformer extends \ItsDamien\Transformer\AbstractTransformer
{
    public function model($model)
    {
        return [
            'first_name' => $model->first_name,
            'last_name'  => $model->last_name,
            'full_name'  => $model->first_name.' '.$model->last_name,
            'foo'        => $this->options['foo'],
        ];
    }
}

class UserTransformer extends \ItsDamien\Transformer\AbstractTransformer
{
    public function model($model)
    {
        return collect([
            'first_name' => $model->first_name,
            'last_name'  => $model->last_name,
            'full_name'  => $model->first_name.' '.$model->last_name,
        ]);
    }
    
    public function withId($model, \Illuminate\Support\Collection $collection)
    {
        return $collection->merge(collect([
            'id' => $model->id,
        ]));
    }
    
    public function withoutFullname($model, \Illuminate\Support\Collection $collection)
    {
        return $collection->except('full_name');
    }
}

return UserTransformer::transform(User::find(1));

// Output:
// {
//     "first_name":"John",
//     "last_name":"Doe",
//     "full_name":"John Doe"
// }

return UserTransformer::transform(User::find(1), [], ['withId']);

// Output:
// {
//     "id":1,
//     "first_name":"John",
//     "last_name":"Doe",
//     "full_name":"John Doe"
// }

return UserTransformer::transform(User::find(1), [], ['withoutFullname']);

// Output:
// {
//     "first_name":"John",
//     "last_name":"Doe",
// }

return UserTransformer::transform(User::find(1), [], ['withId', 'withoutFullname']);

// Output:
// {
//     "id":1,
//     "first_name":"John",
//     "last_name":"Doe",
// }