PHP code example of dilneiss / laravel-api-tool-kit

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

    

dilneiss / laravel-api-tool-kit example snippets



namespace App\Exceptions;

use Essa\APIToolKit\Exceptions\Handler as APIHandler;

class Handler extends APIHandler
{
}


use Essa\APIToolKit\Api\ApiResponse;

class Controller extends BaseController
{
    use ApiResponse;
}

$this->responseSuccess('car created successfully' , $car);


responseSuccess($message , $data)  // returns a 200 HTTP status code
responseCreated($message,$data)  // returns a 201 HTTP status code 
responseDeleted()  // returns empty response with a 204 HTTP status code
responseNotFound($error_details,$error_title)  // returns a 404 HTTP status code
responseBadRequest($error_details,$error_title)  // returns a 400 HTTP status code
responseUnAuthorized($error_details,$error_title)  // returns a 403 HTTP status code
responseConflictError($error_details,$error_title)  // returns a 409 HTTP status code
responseUnprocessable($error_details,$error_title)  // returns a 422 HTTP status code
responseUnAuthenticated ($error_details,$error_title) // returns a 401 HTTP status code
responseWithCustomError($error_title, $error_details, $status_code) //send custom error 

$users = User::dynamicPaginate();

protected $default_filters = CarFilters::class;

Car::useFilters()->get();

Car::useFilters(SpecialCaseCarFilters::class)->get();

//to add the attributes to filter by =>> /cars?color=red&model_id=1
protected array $allowedFilters  = ['color' , 'model_id']; 
//to add the attributes to filter by :
// desc : ?sorts=created_at
// asc  : ?sorts=-created_at
protected array $allowedSorts= ['created_at'];
// allowed relationships to be loaded 
// ?

public function year($term)
{
    $this->builder->whereYear('created_At', $term);
}

//usage : /cars?year=2020
 
public function option($term)
{
    $this->builder->whereHas('options', fn($query) => $query->where('option_id', $term));
}
//usage : /cars?option=1


namespace App\Actions;

class CreateCar
{
    public function execute($data)
    {
      //add business logic to create a car
    }
}

app(CreateCar::class)->execute($data);

private $create_car_action ;

public function __construct(CreateCar $create_car_action)
{
    $this->create_car_action=$create_car_action;
}

public function doSomething()
{
    $this->create_car_action->execute($data);
}

public function doSomething(CreateCar $create_car_action)
{
    $create_car_action->execute($data);
}

// to upload file
$file_path = MediaHelper::uploadFile($file ,$path); 
//to delete an file
MediaHelper::deleteFile($path); 
//upload multiple files
$files_paths = MediaHelper::uploadMultiple($files ,$path); 
//upload base64 image
$image_path = MediaHelper::uploadBase64Image($encoded_image ,$path); 

namespace App\Enums;

class UserTypes extends Enum
{
    public const ADMIN = 'admin';
    public const STUDENT = 'student';
}

UserTypes::getAll() //get all types 
UserTypes::isValid($value) //to check if this value exist in the enum
UserTypes::toArray() //to get all enums as key and value

public function index()
{
    if (auth()->user()->not_active ) {
        $this->responseUnAuthorized('you can not preform this action');
    } 
}

public function index()
{
    if (auth()->user()->not_active ) {
        throw new AuthorizationException('you can not preform this action');
    } 
}

php artisan vendor:publish --provider="Essa\APIToolKit\APIToolKitServiceProvider" --tag="config"

\users?pagination='none'

\users?per_page=10

php artisan api:generate Car

php artisan make:action CreateCar

php artisan make:enum UserTypes