rizkussef/laravel-crud-api 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rizkussef/laravel-crud-api

Composer 安装命令:

composer require rizkussef/laravel-crud-api

包简介

A clean, scalable Api CRUD package for the Service-based architecture for Laravel with advanced filtering, eager-loading relationships, and automatic resource resolution

README 文档

README

Latest Version on Packagist Total Downloads License

A clean, scalable Api CRUD architecture for Laravel, built around a reusable Base Service and Base Controller with automatic:

  • ✅ Model Resolution
  • ✅ Resource Resolution
  • ✅ Collection Handling
  • ✅ Pagination
  • ✅ Advanced Filtering (=, !=, >, <, >=, <=, LIKE, IN, BETWEEN, NULL checks, Date operations)
  • ✅ Eager Loading Relationships
  • ✅ API Response Formatting
  • ✅ Auto-Validation using Form Requests
  • ✅ Auto Creation of Model, Resource, Requests, Controller, Service, Migration using Command

Designed to eliminate repetitive CRUD logic while keeping your application clean and maintainable. Pass filters and relationships directly from your frontend to dynamically build optimized queries with full control over data retrieval.

📦 Installation

Install via Composer:

composer require rizkussef/laravel-crud-api

If needed, manually register the provider in:

config/app.php
Rizkussef\LaravelCrudApi\ApiCrudServiceProvider::class,

⚙️ Publish Configuration

php artisan vendor:publish --provider="Rizkussef\LaravelCrudApi\ApiCrudServiceProvider" --tag=config

This will publish:

config/api-crud.php

Example:

return [
    'paginate' => 15,
];

🏗 Architecture

App
 ├── Models
 │     └── User.php
 ├── Services
 │     └── UserService.php
 ├── Http
 │     ├── Controllers
 │     │      └── UserController.php
 │     ├── Requests
 │     │      ├── UserRequest.php
 │     │      └── UserUpdateRequest.php
 │     └── Resources
 │            └── UserResource.php

⚡ Artisan Generator Command

The absolute fastest way to get started is by using the built-in Artisan generator. This powerful command scaffolds a fully functional CRUD API endpoint in seconds!

php artisan api-crud:make {Model}

🎯 Example: Creating an Article API

php artisan api-crud:make Article

What happens behind the scenes? The command automatically generates all layers required for an API CRUD operation:

Layer Generated File Description
Model App\Models\Article.php The Eloquent model (along with its database migration).
Resource App\Http\Resources\ArticleResource.php Automatically serializes and formats your JSON response.
Requests App\Http\Requests\ArticleRequest.php
ArticleUpdateRequest.php
Dedicated Form validation classes (Auto-validated by Controller).
Service App\Services\ArticleService.php Core business logic layer (Extends ApiCrudService).
Controller App\Http\Controllers\ArticleController.php Handles routing and formatting (Extends ApiCrudController).

⚙️ Available Options

Option Description Example
--no-migration Tells the generator to skip database migration file creation. php artisan api-crud:make User --no-migration

Once generated, just register your new endpoint in routes/api.php and you're fully operational!

Route::apiResource('articles', ArticleController::class);

🛠 Manual Setup (Step-by-Step)

1️⃣ Create a Service

namespace App\Services;

use Rizkussef\LaravelCrudApi\Services\ApiCrudService;

class UserService extends ApiCrudService
{
    // Add custom business logic here if needed
}

🔎 Automatic Resolution (Models, Resources, and Form Requests)

The architecture heavily relies on naming conventions to automatically resolve dependencies:

UserService → App\Models\User
UserService → App\Http\Resources\UserResource
UserController → App\Http\Requests\UserRequest (Used for store)
UserController → App\Http\Requests\UserUpdateRequest (Used for update)

No need to manually inject or define the Model, Resource, or Form Requests. ApiCrudController automatically validates inbound requests using the resolved Form Request before passing data to the Service.

2️⃣ Create a Controller

namespace App\Http\Controllers;

use App\Services\UserService;
use Rizkussef\LaravelCrudApi\Http\Controllers\ApiCrudController;

class UserController extends ApiCrudController {
    public function __construct(UserService $service)
    {
        parent::__construct($service);
    }
}

Just Inject the specific service for this controller.

� Filters & Relationships

The package includes powerful filtering and eager-loading capabilities. Pass filters and relationships from the frontend to dynamically build queries. They can be passed either as array parameters or as JSON-encoded strings.

Available Filter Operators

Comparison Operators

// Equal / Greater Than / Less Than
['age' => ['operator' => '=', 'value' => 25]]
['age' => ['operator' => '>', 'value' => 18]]
['age' => ['operator' => '>=', 'value' => 18]]
['age' => ['operator' => '<', 'value' => 65]]
['age' => ['operator' => '<=', 'value' => 65]]
['status' => ['operator' => '!=', 'value' => 'deleted']]

String Matching

// LIKE search (wildcard)
['name' => ['operator' => 'like', 'value' => 'John']]

// Starts with
['email' => ['operator' => 'starts_with', 'value' => 'admin']]

// Ends with
['email' => ['operator' => 'ends_with', 'value' => '.com']]

// Simple value (auto LIKE for strings)
['name' => 'John']  // Searches LIKE "%John%"

Array Operations

// Multiple values
['status' => ['operator' => 'in', 'value' => ['active', 'pending']]]

// Exclude values
['status' => ['operator' => 'not_in', 'value' => ['deleted', 'banned']]]

// Range
['price' => ['operator' => 'between', 'value' => [100, 500]]]
['price' => ['operator' => 'not_between', 'value' => [100, 500]]]

NULL Checks

['deleted_at' => ['operator' => 'null']]      // IS NULL
['verified_at' => ['operator' => '!null']]    // IS NOT NULL
['verified_at' => ['operator' => 'not_null']]
['published_at' => ['operator' => 'exists', 'value' => true]]   // IS NOT NULL
['published_at' => ['operator' => 'exists', 'value' => false]]  // IS NULL

Date Operations

['created_at' => ['operator' => 'date', 'value' => '2024-02-23']]
['created_at' => ['operator' => 'year', 'value' => 2024]]
['created_at' => ['operator' => 'month', 'value' => 2]]
['created_at' => ['operator' => 'day', 'value' => 23]]

Frontend Examples

Axios Example

import axios from 'axios';

const fetchUsers = async () => {
  const { data } = await axios.get('/api/users/Paginate', {
    params: {
      filters: {
        status: 'active',
        age: { operator: '>', value: 18 }
      },
      relationships: ['profile', 'comments'],
      per_page: 15
    }
  });
  
  console.log(data);
};

Backend Services Examples

Another Service Calling This Service

namespace App\Services;

use App\Services\UserService;

class ReportService
{
    public function __construct(private UserService $userService) {}

    /**
     * Generate active users report
     */
    public function getActiveUsersReport()
    {
        $filters = [
            'status' => 'active',
            'created_at' => [
                'operator' => 'date',
                'value' => now()->subMonth()->format('Y-m-d')
            ]
        ];

        $relationships = ['profile', 'department'];

        return $this->userService->index($filters, $relationships);
    }

    /**
     * Get premium users with pagination
     */
    public function getPremiumUsers($page = 1)
    {
        $filters = [
            'subscription_type' => 'premium',
            'last_payment_date' => [
                'operator' => '!=null'
            ]
        ];

        $relationships = ['subscription', 'profile'];

        return $this->userService->getPaginated(
            perPage: 50,
            filters: $filters,
            relationships: $relationships
        );
    }

    /**
     * Search users by multiple criteria
     */
    public function searchUsers($name, $department, $minAge)
    {
        $filters = [
            'name' => $name,
            'department_id' => $department,
            'age' => [
                'operator' => '>=',
                'value' => $minAge
            ]
        ];

        return $this->userService->index($filters, ['profile', 'department']);
    }
}

�🔁 Built-in CRUD Methods

The BaseCrudController provides:

  • index() – List records
  • getPaginated($perPage) - Paginate list of records
  • show($id) – Show single record
  • store(Request $request) – Automatically resolves and validates the incoming Request using the associated Form Request, then creates record
  • update(Request $request, $id) – Automatically resolves and validates using the associated Update Form Request, then updates record
  • destroy($id) – Delete record

Controller handles validation & response formatting. All business logic is handled inside ApiCrudService.

📄 Automatic Resource Handling

Single Item

return new UserResource($user);

Collection

return UserResource::collection($users);

Automatically handled by the base service.

No Resource

// No UserResource exists
return $user; // returns full user model data

If the corresponding Resource class does not exist, the base controller will return the raw model data instead of a resource.

📊 Example JSON Response

GET /users

{
  "data": [
    {
      "id": 1,
      "name": "John Doe"
    }
  ],
  "meta": {
    "current_page": 1,
    "per_page": 15
  }
}

🧠 Best Practices

✔ Use the Artisan Generator Command to create all layers automatically.
✔ Keep business logic inside Model-specific services.
✔ Keep controllers thin.
✔ Extend ApiCrudService instead of modifying it.
✔ Use Resources for API formatting.
✔ Use filters and relationships to avoid N+1 query problems.

Example 1: Simple Business Logic with Filters

namespace App\Services;

use Rizkussef\LaravelCrudApi\Services\ApiCrudService;

class UserService extends ApiCrudService
{
    /**
     * Activate user
     */
    public function activate($id)
    {
        $user = $this->model->findOrFail($id);
        $user->update(['active' => true]);
        return $this->applyResource($user);
    }

    /**
     * Get active users in a department
     */
    public function getActiveDepartmentUsers($departmentId, $perPage = 15)
    {
        $filters = [
            'active' => true,
            'department_id' => $departmentId
        ];
        
        $relationships = ['department', 'profile'];
        
        return $this->getPaginated($perPage, $filters, $relationships);
    }
}

Example 2: Complex Filtering with Date Range

namespace App\Services;

use Rizkussef\LaravelCrudApi\Services\ApiCrudService;

class OrderService extends ApiCrudService
{
    /**
     * Get high-value orders in date range with customer details
     */
    public function getHighValueOrders($minAmount, $startDate, $endDate, $perPage = 20)
    {
        $filters = [
            'total_amount' => [
                'operator' => '>=',
                'value' => $minAmount
            ],
            'created_at' => [
                'operator' => 'between',
                'value' => [$startDate, $endDate]
            ],
            'status' => [
                'operator' => 'in',
                'value' => ['completed', 'shipped']
            ]
        ];

        $relationships = ['customer', 'items.product', 'payment'];

        return $this->getPaginated($perPage, $filters, $relationships);
    }

    /**
     * Search orders by customer name and status
     */
    public function searchOrders($customerName, $statuses)
    {
        $filters = [
            'customer_name' => [
                'operator' => 'like',
                'value' => $customerName
            ],
            'status' => [
                'operator' => 'in',
                'value' => $statuses
            ]
        ];

        return $this->index($filters, ['customer', 'items']);
    }
}

Example 3: Filtering with NULL Checks

namespace App\Services;

use Rizkussef\LaravelCrudApi\Services\ApiCrudService;

class ArticleService extends ApiCrudService
{
    /**
     * Get published articles with authors and comments
     */
    public function getPublishedArticles($perPage = 15)
    {
        $filters = [
            'status' => 'published',
            'published_at' => [
                'operator' => '!null'  // IS NOT NULL
            ]
        ];

        $relationships = ['author', 'comments.author', 'tags'];

        return $this->getPaginated($perPage, $filters, $relationships);
    }

    /**
     * Get articles pending review (no reviewer assigned)
     */
    public function getPendingReview()
    {
        $filters = [
            'status' => 'draft',
            'reviewer_id' => [
                'operator' => 'null'  // IS NULL
            ]
        ];

        return $this->index($filters, ['author']);
    }
}

🔌 Dependency Injection

ApiCrudService is bound in the container via the Service Provider, so it can be injected anywhere.

🔧 Extending

You can extend:

  • ApiCrudController
  • ApiCrudService
  • Pagination configuration

And you can use:

  • API Response Trait
  • Filter Query Trait
  • Relationship Query Trait

🛣 Example Routes

Route::apiResource('users', UserController::class);

👨‍💻 Author & Support

Developed by Rizk Ussef

A fullstack developer passionate about creating clean, maintainable architecture patterns and reusable solutions for building scalable applications across frontend and backend ecosystems.

Get in Touch

🤝 Contributing

Contributions are welcome! If you find bugs or have feature suggestions, please open an issue or submit a pull request.

📄 License

MIT License - see LICENSE file for details.

rizkussef/laravel-crud-api 适用场景与选型建议

rizkussef/laravel-crud-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「rest」 「api」 「pagination」 「crud」 「laravel」 「Relationships」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 rizkussef/laravel-crud-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rizkussef/laravel-crud-api 我们能提供哪些服务?
定制开发 / 二次开发

基于 rizkussef/laravel-crud-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 39
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-09