PHP code example of supaapps / supaapps-laravel-api-kit

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

    

supaapps / supaapps-laravel-api-kit example snippets


composer 

use Supaapps\LaravelApiKit\Controllers\BaseCrudController;

class ExampleController extends BaseCrudController
{
    public string $model = \App\Models\Example::class; // replace with your model
}

public string $model = \App\Models\Example::class; // replace with your model

public bool $shouldPaginate = false;

public ?string $searchField = null; // replace with desired column

public array $searchSimilarFields = [];

// user hit endpoint > /example?search=supa

public array $searchSimilarFields = [
    'name',
    'description',
];

public array $searchExactFields = [];

// user hit endpoint > /example?search=1

public array $searchExactFields = [
    'id',
    'price',
    'category_id',
];

public array $searchDateFields = [];

// user hit endpoint > /example?search=2023-09-26

public array $searchDateFields = [
    'completed_at',
    'created_at',
    'updated_at',
];

public array $filters = [];

// user hit endpoint > /example?ids[]=1&ids[]=2&codes[]=ABC

public array $filters = [
    'ids',
    'codes',
];

public array $dateFilters = [];

// user hit endpoint > /example?created_at_min=2023-09-01&created_at_max=2023-09-30&updated_at_min=2023-09-15

public array $dateFilters = [
    'created_at',
    'updated_at',
];

public ?array $isEmptyFilters = [];

// user hit endpoint > /example?is_empty[completed_at]=false&is_empty[cancelled_at]=true

public ?array $isEmptyFilters = [
    'completed_at',
    'cancelled_at',
];

public ?array $defaultOrderByColumns = null;

public ?array $defaultOrderByColumns = [
    'created_at,desc',
    'id,asc'
];

public bool $readOnly = false;

public bool $isDeletable = false;

private function getSearchExactFields(): array
{
    if (request('user_type') == 'admin') {
        return [
            'admin_id'
        ];
    }

    return [
        'user_id'
    ];
}

private function getSearchSimilarFields(): array;

private function getSearchExactFields(): array;

private function getSearchDateFields(): array;

private function getFilters(): array;

private function getDateFilters(): array;

private function getIsEmptyFilters(): array;

// by default, it merges the values from:
//    $this->getSearchSimilarFields(),
//    $this->getSearchExactFields(),
//    $this->getSearchDateFields()
private function getOrderByColumns(): array;

private function getDefaultOrderByColumns(): ?array;

Schema::table('articles', function (Blueprint $table) {
    $table->auditIds();
});

protected $fillable = [
    'created_by_id',
    'updated_by_id',
    ...
]

// in src/Providers/EventServiceProvider.php add the following line

protected $observers = [
    \App\Models\Article::class => [ // replace Article with your model
        \Supaapps\LaravelApiKit\Observers\UserSignatureObserver::class,
    ],
    ...
];
sh
php artisan make:crud-controller