mehedi8gb/api-crudify

Automate CRUD operations for Laravel APIs.

Maintainers

Package info

github.com/mehedi8gb/api-crudify

pkg:composer/mehedi8gb/api-crudify

Statistics

Installs: 438

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0


README

Laravel API Engine for Scalable Query-Driven Applications

Latest Version on Packagist Total Downloads License

Api Crudify is a professional-grade Laravel package that automates generation of robust, scalable, and standardized API CRUD components. It enforces the Service-Repository design pattern and implements a Chain of Responsibility pipeline for complex API queries β€” filtering, sorting, relation loading, soft deletes, and pagination β€” all driven by query parameters.

πŸš€ Key Features

  • Standardized Architecture β€” Generates Controller, Service, Repository, Model, FormRequests, Resource, Migration, Factory, Seeder, and Feature Test in one command.
  • Chain of Responsibility Query Pipeline β€” Modular, ordered handlers process every API request: SoftDelete β†’ Relations β†’ Filter β†’ Sort β†’ Pagination.
  • Advanced Dynamic Filtering β€” Frontend-friendly ?q= shorthand plus low-level ?where= and ?orWhere= with relation traversal support.
  • Smart Relation Loading β€” Respects explicitly passed relations, model $with, or Eloquent eager loads automatically.
  • Soft Delete Awareness β€” ?trashed=with or ?trashed=only query params built in.
  • Domain-Driven Design Ready β€” Full support for nested namespaces: V1/Inventory/Product.
  • Auto-Restoration β€” Detects and restores any missing base classes on every crudify:make run.
  • Route Management β€” Automatically registers API routes and use statements.
  • Helper Utilities β€” Global helper functions for responses, caching, payload filtering, and more.

πŸ“¦ Installation

composer require mehedi8gb/api-crudify --dev

Run the installer to bootstrap all base classes and configure autoloading:

php artisan crudify:install

This command will:

  • Copy all base classes, interfaces, and query handlers into your app/ directory
  • Add app/Helpers/Helpers.php to composer.json autoload files
  • Run composer dump-autoload automatically

πŸ›  Usage

php artisan crudify:make {Name}

Examples

# Simple CRUD
php artisan crudify:make Product

# Versioned / domain-specific
php artisan crudify:make V1/Inventory/Category

# With Postman schema export
php artisan crudify:make Product --export-api-schema

πŸ“‚ Generated Components

Component Path
Controller app/Http/Controllers/{Path}/{Name}Controller.php
Model app/Models/{Path}/{Name}.php
Service app/Services/{Path}/{Name}Service.php
Repository app/Repositories/{Path}/{Name}Repository.php
Form Requests app/Http/Requests/{Path}/{Name}StoreRequest.php & UpdateRequest.php
Resource app/Http/Resources/{Path}/{Name}Resource.php
Migration database/migrations/YYYY_MM_DD_create_{names}_table.php
Factory database/factories/{Path}/{Name}Factory.php
Seeder database/seeders/{Path}/{Name}Seeder.php
Feature Test tests/Feature/{Path}/{Name}Test.php

πŸ— Architecture Overview

Every generated CRUD follows a strict 3-layer architecture:

HTTP Request
     β”‚
     β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         Controller              β”‚  ← HTTP only: validate, call service, return response
β”‚   (extends BaseController)      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚
               β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           Service               β”‚  ← Business logic, orchestration
β”‚   (extends BaseService)         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚
               β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚          Repository             β”‚  ← Data access only, Eloquent queries
β”‚   (extends BaseRepository)      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚
               β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     Query Pipeline              β”‚  ← Chain of Responsibility
β”‚  SoftDelete β†’ Relations β†’       β”‚
β”‚  Filter β†’ Sort β†’ Pagination     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Real-World Example

Controller β€” HTTP only, no business logic:

final class ProductController extends BaseController
{
    public function __construct(
        private readonly ProductService $service
    ) {}

    public function index(): JsonResponse
    {
        $collection = $this->service->getProductsCollection();
        return $this->successResponse('Products retrieved successfully', $collection);
    }
}

Service β€” orchestrates business logic:

class ProductService extends BaseService
{
    public function __construct(
        protected ProductRepository $productRepository,
        protected Request $request
    ) {
        parent::__construct($productRepository, $request);
    }

    public function getProductsCollection(): array
    {
        $data = $this->productRepository->getProductsData();
        return $this->prepareResourceResponse($data, ProductResource::class);
    }
}

Repository β€” data access only:

class ProductRepository extends BaseRepository
{
    public function __construct(Product $model, protected Request $request)
    {
        parent::__construct($model, $request);
    }

    public function getProductsData(array $with = ['category', 'tags']): array
    {
        return $this->handleApiQueryRequest($this->query(), $with);
    }
}

⚑ Query Pipeline β€” Chain of Responsibility

Every API request through handleApiQueryRequest() passes through this ordered pipeline:

Builder + Request
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ SoftDeleteHandlerβ”‚  ?trashed=with|only|default
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  RelationHandler β”‚  Explicit $with β†’ model $with β†’ Eloquent eager loads
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  FilterHandler   β”‚  ?q= / ?where= / ?orWhere= / ?exclude= / ?operator=
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   SortHandler    β”‚  ?sortBy= / ?sortOrder=asc|desc
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚PaginationHandler β”‚  ?page= / ?limit= / ?limit=all
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β–Ό
  { meta, data }

πŸ” Query Parameter Reference

Filtering

Parameter Description Example
?q= Frontend-friendly shorthand filter ?q=title=phone
?where= Direct column filter ?where=status,active
?orWhere= OR column filter ?orWhere=type,admin
?operator= Comparison operator ?operator== or ?operator=like
?exclude= Exclude a specific value ?exclude=status,deleted

Relation filtering via ?where=:

?where=with:category,name,Electronics
?where=with:category.parent,name,Tech

Frontend shorthand ?q= with pipe-separated conditions:

?q=title=phone|status=active
?q=category.name=Electronics

Complex nested relational query conditions:

?q=posts.category.child.status=active
?q=posts.createdBy.userId=10

?or=true converts multiple ?q= conditions to orWhere:

?q=title=phone|brand=apple&or=true

Sorting

Parameter Default Example
?sortBy= created_at ?sortBy=price
?sortOrder= desc ?sortOrder=asc

Safe against SQL injection β€” column existence is verified against the schema before applying. Skipped if the builder already has an orderBy applied.

Soft Deletes

Parameter Behaviour
?trashed=with Include soft-deleted records
?trashed=only Return only soft-deleted records
(omitted) Exclude soft-deleted records (default)

Pagination

Parameter Default Description
?page= 1 Current page
?limit= 10 Records per page
?limit=all β€” Returns all records, no pagination

Response structure:

{
  "meta": {
    "page": 1,
    "limit": 10,
    "total": 245,
    "totalPage": 25
  },
  "data": [ ... ]
}

Relation Loading

Relations are resolved in this priority order:

  1. Explicit $with array passed to handleApiQueryRequest()
  2. Model-level $with property
  3. Eloquent registered eager loads via getEagerLoads()

🧰 Helper Functions

Global utility functions available after install:

Function Description
sendSuccessResponse($message, $data, $status) Standardized JSON success response
sendErrorResponse($exception, $status) Standardized JSON error response with environment-aware detail
validationException($payload, $key) Throw a ValidationException from array or string
filterPayload($data, $allowedKeys) Keep only allowed keys from an array
cacheQuery($query, $method, $args, $ttl) Execute and cache a query with tag-based invalidation
generateCacheKey($builder, $request, $tenantId) Generate deterministic cache key from query state
getCreatedAtColumn($builder|$model) Resolve model's CREATED_AT column safely
getResourceClass($model) Auto-resolve {Model}Resource class or fallback to DefaultResource
deepMerge($original, $new) Deep array merge with force-replace support
processNestedArray($existing, $payload) Merge and deduplicate nested arrays by id
convertStatus($status) Convert boolean to 1/0
generateUniqueNumber($prefix) Generate a unique prefixed identifier
getFormatedDate($carbon) Human-readable date with diff: "2 days ago (14th April at 08:21 AM in 2025)"

βš™οΈ Base Classes Installed

After crudify:install, the following are placed in your app/ directory:

app/
β”œβ”€β”€ IContracts/
β”‚   β”œβ”€β”€ Repositories/  (IRepository, IReadRepository, IWriteRepository)
β”‚   └── Services/      (IService, IReadService, IWriteService)
β”œβ”€β”€ Repositories/V1/BaseRepository.php
β”œβ”€β”€ Services/V1/BaseService.php
β”œβ”€β”€ Models/Model.php
β”œβ”€β”€ Helpers/Helpers.php
β”œβ”€β”€ Core/
β”‚   β”œβ”€β”€ Query/
β”‚   β”‚   β”œβ”€β”€ HandleApiQueryRequest.php
β”‚   β”‚   β”œβ”€β”€ Contracts/IQueryHandler.php
β”‚   β”‚   └── Handlers/
β”‚   β”‚       β”œβ”€β”€ AbstractQueryHandler.php
β”‚   β”‚       β”œβ”€β”€ Core/
β”‚   β”‚       β”‚   β”œβ”€β”€ FilterHandler.php
β”‚   β”‚       β”‚   β”œβ”€β”€ PaginationHandler.php
β”‚   β”‚       β”‚   β”œβ”€β”€ RelationHandler.php
β”‚   β”‚       β”‚   β”œβ”€β”€ SoftDeleteHandler.php
β”‚   β”‚       β”‚   └── SortHandler.php
β”‚   β”‚       └── Optimization/CacheHandler.php
β”‚   └── ClientQuery/   (mirror of Query for client-facing APIs)
└── Http/Resources/DefaultResource.php

πŸ“œ Changelog

Please see CHANGELOG for recent changes.

✨ Credits

πŸ“œ License

The MIT License (MIT). Please see License File for more information.