PHPackages                             dilneiss/laravel-api-tool-kit - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. dilneiss/laravel-api-tool-kit

ActiveLibrary[API Development](/categories/api)

dilneiss/laravel-api-tool-kit
=============================

set of tools to build an api with laravel

v1.3(4y ago)04MITPHPPHP ^7.4|^8.0

Since Nov 7Pushed 4y agoCompare

[ Source](https://github.com/dilneiss/laravel-api-tool-kit)[ Packagist](https://packagist.org/packages/dilneiss/laravel-api-tool-kit)[ Docs](https://github.com/ahmedesa/laravel-api-tool-kit)[ RSS](/packages/dilneiss-laravel-api-tool-kit/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (1)Versions (10)Used By (0)

Laravel API tool kit
====================

[](#laravel-api-tool-kit)

[![](laravel-api-tool-kit.png)](laravel-api-tool-kit.png)

Laravel api tool kit is a set of tools that will help you to build a fast and well-organized API using laravel best practices.

Contents
--------

[](#contents)

[Installation](#installation)

[Api response](#api-response)

[Dynamic Pagination](#dynamic-pagination)

[Filters](#filters)

[Api Generator](#api-generator)

[Actions](#actions)

[Media Helper](#media-helper)

[Enum](#enum)

[General tips](#general-tips)

**Installation**
----------------

[](#installation)

```
composer require essa/api-tool-kit

```

to publish config

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

```

use exception handler to standardize the error response [Error Response](#error-response)

in App\\Exceptions\\Handler class extend the APIHandler class

```
namespace App\Exceptions;

use Essa\APIToolKit\Exceptions\Handler as APIHandler;

class Handler extends APIHandler
{
}
```

use API Response Trait in Controller

`App\Http\Controllers\Controller.php`:

```
use Essa\APIToolKit\Api\ApiResponse;

class Controller extends BaseController
{
    use ApiResponse;
}
```

check : [API response](#api-response)

[🔝 Back to contents](#contents)

**API Response**
----------------

[](#api-response)

it is used to format your response to standard format and status codes for success responses, it will be

#### **Success Response**

[](#success-response)

```
{
  "message": "your message",
  "data": "your date"
}
```

#### **Error Response**

[](#error-response)

```
{
  "errors": [
    {
      "status": 403,
      "title": "unauthenticated!",
      "detail": "Unauthenticated."
    }
  ]
}
```

usage: you can use the trait inside the class you want to return the response form

and use it like this

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

Available Methods

```
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
```

[🔝 Back to contents](#contents)

**Dynamic Pagination**
----------------------

[](#dynamic-pagination)

use pagination dynamically

#### usage

[](#usage)

to use dynamic pagination to get all users :

```
$users = User::dynamicPaginate();
```

to get all users without pagination :

```
\users?pagination='none'

```

to get all users paginated 10 users per page:

```
\users?per_page=10

```

by default pagination is 20 element per page you can change the default value from config/api-tool-kit

**Filters**
-----------

[](#filters)

usage:

to create a filter class:

```
php artisan make:filter CarFilters

```

to set default filters to the Car model , in Car model you will add

```
protected $default_filters = CarFilters::class;
```

to use it

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

if you want to override the default filters

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

options in Filter class

```
//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
// ?includes=model
protected array $allowedIncludes = ['model'];
//column that will be included in search =>> ?search=tesla
protected array $columnSearch= ['name','descriptions'];
//relation that will be included in search =>> ?search=ahmed
protected array $relationSearch = [
    'user' => ['first_name', 'last_name']
];
```

to create a custom query you will just create a new function in the class and add your query example filter by year:

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

//usage : /cars?year=2020
```

filter by relationship :

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

[🔝 Back to contents](#contents)

**API Generator**
-----------------

[](#api-generator)

#### Usage :

[](#usage-)

```
php artisan api:generate Car

```

when you type the command it will ask you whether you want default options :

- (N) it will ask you which files you want to generate .
- (Y) it will generate files for all options that exists in config/api-tool-kit

##### options :

[](#options-)

```
 ** by default it will create a model :
-app/Models/Car.php
 ** controller :
-app/Http/Controllers/API/CarController.php
 ** resource :
-app/Http/Resources/CarResource.php
 ** request :
-app/Http/Requests/Car/CreateCarRequest.php
-app/Http/Requests/Car/UpdateCarRequest.php
 ** filter :
-app/Filters/CarFilters.php
 ** seeder :
-database/seeders/CarSeeder.php
 ** factory :
-database/factories/CarFactory.php
 ** test :
-Tests/Feature/CarTest.php
 ** migration :
-database/migrations/x_x_x_x_create_cars_table.php

```

in addition, the routes will be created and added in routes/api.php files

[🔝 Back to contents](#contents)

**Actions**
-----------

[](#actions)

action is a laravel implementation of command design pattern which create a class where you can add your business logic in [https://en.wikipedia.org/wiki/Command\_pattern](https://en.wikipedia.org/wiki/Command_pattern)

usage:

```
php artisan make:action CreateCar

```

```
