PHP code example of coreproc / api-builder

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

    

coreproc / api-builder example snippets




namespace App\Http\Controllers\Api;

use App\Models\Post;
use App\Transformers\PostTransformer;
use CoreProc\ApiBuilder\Http\Controllers\ApiBuilderController;

class PostsController extends ApiBuilderController
{
    public static $model = Post::class;

    public static $transformer = PostTransformer::class;

    protected $allowedParams = [
        'user_id',
        'title',
        'short_description',
        'content',
        'published_at',
    ];
}

Route::apiResource('posts', 'Api\PostsController');

GET     /api/posts          Get a paginated list of the posts in your database
POST    /api/posts          Create a new Post entry
GET     /api/posts/{id}     Get the details of the specified Post resource
PUT     /api/posts/{id}     Update the Post resource
DEL     /api/posts/{id}     Delete the Post resource

class PostsController extends ApiBuilderController {

    ...
    
    protected static function indexQuery(Request $request, Builder $query)
    {
        return $query->where('user_id', $request->user()->id);
    }

class PostsController extends ApiBuilderController {

    ...
    
    protected $dates = [
        'published_at',
    ];

GET /api/posts?user_id=1

GET /api/posts?user_id_gt=5

GET /api/posts?limit=1

GET /api/posts?page=2