ngoctp/laravel-easy-jsonapi

Follow Extended Fractal package for smarter usage for API response

v1.0.5 2018-08-29 10:59 UTC

This package is not auto-updated.

Last update: 2024-11-10 06:02:15 UTC


README

This is an extended library for responding common API easily.

This package supports Laravel 5.0 and newer versions.

You can also use this package with Lumen.

Installation

Import package using composer:

composer require ngoctp/laravel-easy-jsonapi

Usages

New options parameter for transformer

With new options parameter in constructor, you can respond more fields optionally in each action

class UserTransformer extends ExtendedTransformerAbstract
{

    protected $name = 'user';

    protected $availableIncludes = [
        'roles',
    ];

    /**
     * @param User $user
     * @return array
     */
    public function transform($user)
    {
        $data = [
            'id' => $user->id,
            'name' => $user->name,
        ];
        
        if (array_get($this->options, 'respond_birthday')) {
            $data['birthday'] = $user->birthday;
        }
        
        return $data;
    }

    public function includeRoles($user)
    {
        return new Collection($user->roles, new RoleTransformer(array_get($this->options, 'roles')), 'role');
    }
}

// Controller
class UserController extends Controller {
    public function index($request) {
        $users = \App\Models\User::get();
        
        return eja_data($users, new \App\Transformers\UserTransformer(
            [
                'respond_birthday' => true,
                'roles' => [
                    'respond_name' => false,
                ]
            ]
        ));
    }
}

Respond success message

class UserController extends Controller {
    public function store($request) {
        // ...
        
        return eja_success('Created user successfully');
    }
}

Respond error message

class UserController extends Controller {
    public function update($request) {
        $validUsername = false;
        
        if (!$validUsername) {
            return eja_error('Username is not valid');
        }
        
        return eja_success('Updated user successfully');
    }
}

Respond normal form error message

class UserController extends Controller {
    public function update($request) {
        $validator = Validator::make($inputs, $rules);
        
        if ($validator->fails()) {
            return eja_form_error($validator);
        }
        
        return eja_success('Updated user successfully');
    }
}

Respond data

class UserController extends Controller {
    public function index($request) {
        $users = \App\Models\User::get();
        //$users = \App\Models\User::paginate(10);
        
        return eja_data($users, new \App\Transformers\UserTransformer());
    }
}

Happy coding :)