PHP code example of ijodkor / laravel-api-response

1. Go to this page and download the library: Download ijodkor/laravel-api-response 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/ */

    

ijodkor / laravel-api-response example snippets


use Ijodkor\ApiResponse\Responses\RestResponse;

class Controller extends Controller {
    use RestResponse;
}

...

class UserController extends Controller {
    public function show() {
        return $this->success([
            'user' => new User();
        ]);
    }
}

use Ijodkor\ApiResponse\Requests\RestRequest;

// class UserRequest extends FormRequest - x
class UserRequest extends RestRequest {

}

use Ijodkor\ApiResponse\Requests\PaginationRequest;

// class UserRequest extends FormRequest - xxx
class UserRequest extends PaginationRequest {

}

use Ijodkor\ApiResponse\Requests\BuilderPaginator;

class UserService {
    
    public function all() {
        // Paginate users
        $users = User::query()->paginate();
        $items = $users->items();
        
        // Change content of paginated list
        $data = collect($items)->map(function(User $user) {
            return [
                'id' => $user->id,
                'name' => $user->name,
            ];
        });
        
        return new BuilderPaginator($users, $data);
    }
}


class UserController extends Controller {
    ...
    
    public function show() {
        $users = $this->service->all();
        
        // paged/paginated
        return $this->paged('users', $users, []);
    }
}