PHP code example of izupet / laravel-api

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

    

izupet / laravel-api example snippets


Izupet\Api\Providers\ApiServiceProvider::class


use App\Http\Requests\CarsRequest;
use Izupet\Api\Traits\ApiResponse;

class CarsController extends Controller
{
    use ApiResponse;
    
    public function index(CarsRequest $request)
    {
        //
        
        return $this->respond('Ok', 200, []);
    }
}

public function getRules()
{
    return $this
        ->search(['model', 'brand'])
        ->pagination()
        ->fields(['id', 'model', 'brand', 'type', 'createdAt', 'images' => [
            'path', 'id', 'position', 'createdAt'
        ]])
        ->order(['model', 'brand', 'id'])
        ->filter([
            'type'                => 'in:coupe,convertable,suv,sedan',
            'images_position'     => 'numeric|min:0|max:4',
            'images_position_nq'  => 'numeric|min:0|max:4',
            'images_path'         => 'max:99',
            'brand'               => 'alpha',
            'images_createdAt_gt' => 'date_format:Y-m-d',
            'createdAt_lt'        => 'date_format:Y-m-d',
            'id'                  => 'numeric'
        ]);
}

array:6 [▼
  "fields" => array:2 [▶]
  "order" => array:2 [▶]
  "search" => array:2 [▶]
  "pagination" => array:2 [▶]
  "filter" => array:2 [▶]
  "body" => array:1 [▶]
]

class CarRepository
{
    public function __construct(
        \App\Car $car
    )
    {
        $this->car = $car;
    }

    public function all(array $input)
    {
        return $this->car->apiGet('fields', 'pagination', 'search', 'order', 'filter', 'relations', $input);
    }

    public function create(array $input)
    {
        return $this->car->apiCreate('fields', $input);
    }

    public function update(array $input, \App\Car $car)
    {
        return $car->apiUpdate('fields', 'relations', $input);
    }

    public function delete(\App\Car $car)
    {
        return $car->apiDelete();
    }
}

\App\Cars::apiGet('fields', 'pagination', 'search', 'order', 'filter', 'relations', $input)

    $cars = \App\Cars::apiFields($input['fields']['select'], true)
        ->apiPagination($input['pagination'])
        ->apiSearch($input['search'])
        ->apiOrder($input['order'])
        ->apiFilter($input['filter']['select']);
    
   $cars->apiRelations($cars, $input['fields']['relations'], $input['filter']['relations']);

public function create(array $input)
{
    return $this->car->apiCreate('fields', $input);
}

public function update(array $input, \App\Car $car)
{
    return $car->apiUpdate('fields', 'relations', $input);
}

public function delete(\App\Car $car)
{
    return $car->apiDelete();
}
bash
$ php artisan vendor:publish
bash
$ php artisan make:controller CarsController
bash
$ php artisan api:request CarsRequest
url
/cars?type=sedan&images.createdAt.gt=2017-01-01