yassin-ahmed/laravel-api-crud-generator

A comprehensive CRUD generator for Laravel API development

Installs: 38

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/yassin-ahmed/laravel-api-crud-generator

v1.0.1 2025-06-19 01:43 UTC

This package is auto-updated.

Last update: 2025-12-19 02:49:41 UTC


README

A powerful Laravel package that generates complete CRUD APIs with a single command. This package creates everything you need for a fully functional REST API including models, controllers, requests, resources, migrations, factories, seeders, tests, and routes.

๐Ÿš€ Features

  • Complete CRUD Generation: Models, Controllers, Requests, Resources, Migrations, Factories, Seeders, Tests, and Routes
  • Field Type Support: String, text, integer, boolean, date, email, JSON, decimal, float, and more
  • Relationship Support: BelongsTo and HasMany relationships
  • Validation: Automatic validation rules generation based on field types
  • API Resources: JSON API responses with proper formatting
  • Pagination: Built-in pagination support with meta information
  • Search & Filtering: Search functionality and sorting options
  • Factory & Seeders: Automatic fake data generation for testing
  • Feature Tests: Complete test suite for all CRUD operations
  • Smart Fake Data: Context-aware fake data generation (emails, names, phones, etc.)

๐Ÿ“ฆ Installation

Install the package via Composer:

composer require yassin-ahmed/laravel-api-crud-generator

Publish the package configuration (optional):

php artisan vendor:publish --provider="Yassin\LaravelApiCrudGenerator\ServiceProvider"

๐ŸŽฏ Quick Start

Generate a complete CRUD API with a single command:

php artisan crud:generate Post --fields="title:string,content:text,published:boolean,published_at:date:nullable"

This creates:

  • Migration file
  • Post model
  • PostController (API)
  • Store/Update request classes
  • PostResource
  • API routes
  • PostFactory
  • PostSeeder
  • Feature tests

๐Ÿ“ Usage

Basic Usage

# Simple model with basic fields
php artisan crud:generate Product --fields="name:string,price:decimal,description:text"

# With nullable fields
php artisan crud:generate User --fields="name:string,email:email,phone:string:nullable,bio:text:nullable"

# With relationships
php artisan crud:generate Post --fields="title:string,content:text" --relations="belongsTo:User,hasMany:Comment"

Command Options

Option Description Example
--fields Define model fields with types --fields="name:string,age:integer"
--relations Define model relationships --relations="belongsTo:User,hasMany:Post"
--force Overwrite existing files --force

Field Types

Type Description Validation Rule
string Short text (255 chars) string|max:255
text Long text string
integer Whole numbers integer
boolean True/false values boolean
date Date values date
email Email addresses email
json JSON data json
decimal Decimal numbers numeric
float Floating point numbers numeric

Relationship Types

Type Description Example
belongsTo Many-to-one relationship belongsTo:User
hasMany One-to-many relationship hasMany:Comment

๐Ÿ“š Examples

E-commerce Product

php artisan crud:generate Product \
  --fields="name:string,description:text,price:decimal,stock:integer,is_active:boolean,category_id:integer" \
  --relations="belongsTo:Category,hasMany:OrderItem"

Blog System

# Create Category
php artisan crud:generate Category --fields="name:string,slug:string,description:text:nullable"

# Create Post with relationship
php artisan crud:generate Post \
  --fields="title:string,slug:string,content:text,excerpt:text:nullable,published_at:date:nullable,is_published:boolean" \
  --relations="belongsTo:User,belongsTo:Category,hasMany:Comment"

# Create Comment
php artisan crud:generate Comment \
  --fields="content:text,author_name:string,author_email:email" \
  --relations="belongsTo:Post"

User Management

php artisan crud:generate Profile \
  --fields="first_name:string,last_name:string,bio:text:nullable,avatar:string:nullable,phone:string:nullable,date_of_birth:date:nullable" \
  --relations="belongsTo:User"

๐Ÿ”ง Generated Files Structure

After running the command, the following files are created:

app/
โ”œโ”€โ”€ Models/
โ”‚   โ””โ”€โ”€ YourModel.php
โ”œโ”€โ”€ Http/
โ”‚   โ”œโ”€โ”€ Controllers/Api/
โ”‚   โ”‚   โ””โ”€โ”€ YourModelController.php
โ”‚   โ”œโ”€โ”€ Requests/YourModel/
โ”‚   โ”‚   โ”œโ”€โ”€ StoreYourModelRequest.php
โ”‚   โ”‚   โ””โ”€โ”€ UpdateYourModelRequest.php
โ”‚   โ””โ”€โ”€ Resources/
โ”‚       โ””โ”€โ”€ YourModelResource.php
database/
โ”œโ”€โ”€ migrations/
โ”‚   โ””โ”€โ”€ xxxx_xx_xx_xxxxxx_create_your_models_table.php
โ”œโ”€โ”€ factories/
โ”‚   โ””โ”€โ”€ YourModelFactory.php
โ””โ”€โ”€ seeders/
    โ””โ”€โ”€ YourModelSeeder.php
routes/api/
โ””โ”€โ”€ YourModel.php
tests/Feature/YourModel/
โ””โ”€โ”€ YourModelApiTest.php

๐ŸŒ API Endpoints

The generated controller provides these endpoints:

Method Endpoint Description
GET /api/your-models List all records (with pagination)
POST /api/your-models Create new record
GET /api/your-models/{id} Show specific record
PUT/PATCH /api/your-models/{id} Update record
DELETE /api/your-models/{id} Delete record

Query Parameters

  • search - Search in name field
  • sort_by - Field to sort by
  • sort_direction - Sort direction (asc/desc)
  • per_page - Records per page (default: 15)

Example API Calls

# List products with pagination
GET /api/products?per_page=10&page=1

# Search products
GET /api/products?search=laptop

# Sort products by price
GET /api/products?sort_by=price&sort_direction=desc

# Create product
POST /api/products
{
    "name": "Laptop",
    "price": 999.99,
    "description": "High-performance laptop"
}

# Update product
PUT /api/products/1
{
    "name": "Gaming Laptop",
    "price": 1299.99
}

๐Ÿงช Testing

The package generates comprehensive feature tests. Run them with:

# Run all tests
php artisan test

# Run specific model tests
php artisan test tests/Feature/Product/ProductApiTest.php

# Run with coverage
php artisan test --coverage

๐Ÿ”„ After Generation

  1. Run migrations:

    php artisan migrate
  2. Register routes (if not using automatic discovery):

    // In routes/api.php
    require_once __DIR__ . '/api/Product.php';
  3. Run seeders (optional):

    php artisan db:seed --class=ProductSeeder
  4. Customize as needed:

    • Update validation rules in request classes
    • Modify API resource fields
    • Add custom methods to controllers
    • Enhance factory definitions

โšก Advanced Features

Custom Validation

Update the generated request classes to add custom validation:

// In StoreProductRequest.php
public function rules()
{
    return [
        'name' => 'required|string|max:255|unique:products',
        'price' => 'required|numeric|min:0',
        'description' => 'nullable|string|max:1000',
    ];
}

Custom API Resources

Enhance the generated resource classes:

// In ProductResource.php
public function toArray($request)
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'price' => number_format($this->price, 2),
        'description' => $this->description,
        'formatted_price' => '$' . number_format($this->price, 2),
        'category' => new CategoryResource($this->whenLoaded('category')),
        'created_at' => $this->created_at->format('Y-m-d H:i:s'),
        'updated_at' => $this->updated_at->format('Y-m-d H:i:s'),
    ];
}

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

๐Ÿ“„ License

This package is open-sourced software licensed under the MIT license.

๐Ÿ“ž Support

If you encounter any issues or have questions:

  1. Check the issues page
  2. Create a new issue if your problem isn't already reported
  3. Provide detailed information about your Laravel version and the command you used

๐Ÿ™ Credits

Created by Yassin

Made with โค๏ธ for the Laravel community