PHP code example of mehedi8gb / api-crudify

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

    

mehedi8gb / api-crudify example snippets


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

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

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);
    }
}

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);
    }
}
bash
php artisan crudify:install
bash
php artisan crudify:make {Name}

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