PHPackages                             melsaka/api-builder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. melsaka/api-builder

ActiveLibrary[API Development](/categories/api)

melsaka/api-builder
===================

Scaffold CRUD API (Controller, Service, Repository, Resource, Requests, Policies, etc.) for Laravel applications

v1.0.1(8mo ago)2181MITPHPPHP ^8.2

Since Aug 19Pushed 8mo agoCompare

[ Source](https://github.com/melsaka/api-builder)[ Packagist](https://packagist.org/packages/melsaka/api-builder)[ Docs](https://github.com/melsaka/api-builder)[ RSS](/packages/melsaka-api-builder/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (3)Used By (0)

Laravel API Builder
===================

[](#laravel-api-builder)

Scaffold CRUD API (Controller, Service, Repository, Resource, Requests, Policies, etc.) for Laravel applications.

Features
--------

[](#features)

- Generate complete CRUD API structure
- Create Controllers, Services, Repositories
- Generate Resources and Form Requests
- Create Policies for authorization
- Includes API error handling traits
- Custom exceptions for API responses
- Supports uploading images to your models

This package is using my [laravel image manager](https://github.com/melsaka/laravel-image-manager) package to support adding images easily to your models.

Requirements
------------

[](#requirements)

- PHP ^8.2
- Laravel ^11.0 or ^12.0

Installation
------------

[](#installation)

You can install the package via Composer:

```
composer require melsaka/api-builder
```

The package will automatically register its service provider.

Usage
-----

[](#usage)

### Publish Stubs

[](#publish-stubs)

If you want to customize the generated files, publish the stub files:

```
php artisan vendor:publish --tag=api-builder-stubs
php artisan vendor:publish --tag=image-manager
```

### Generate Required Files

[](#generate-required-files)

First, generate the base API files (controllers, traits, exceptions):

```
php artisan api:scaffold
```

### Generate CRUD for a Model

[](#generate-crud-for-a-model)

Generate complete CRUD for a specific model:

```
php artisan api:crud User
```

This will create:

- Controller: `app/Http/Controllers/Api/User/UserController.php`
- Service: `app/Services/UserService.php`
- Repository: `app/Repositories/UserRepository.php`
- Resource: `app/Http/Resources/UserResource.php`
- Requests: `app/Http/Requests/User/StoreUserRequest.php` &amp; `UpdateUserRequest.php`
- Policy: `app/Policies/UserPolicy.php`
- Routes: `app/Routes/User.php`

### Uploading Images To Your Models

[](#uploading-images-to-your-models)

Checkout [laravel image manager](https://github.com/melsaka/laravel-image-manager) package to learn what you need to do first.

Publish the configuration file:

```
php artisan vendor:publish --provider="Melsaka\ImageManager\ImageManagerServiceProvider" --tag="image-manager"
```

Then you create the images table:

```
php artisan migrate
```

Add your model name and it's settings under the models attribute in the config file:

```
return [
    'storage_disk' => 'public', // could be r2, aws, etc..
    'base_path' => 'uploads',
    'format' => 'webp',
    'quality' => 90,

    'models' => [
        'user' => [
            'types' => [
                'avatar' => [
                    'sizes' => [
                        'thumbnail' => [
                            'width' => 100,
                            'height' => 100,
                            'mode' => 'cover',
                        ],
                        'medium' => [
                            'width' => 300,
                            'height' => 300,
                            'mode' => 'cover',
                        ],
                    ],
                ],
            ],
        ],
    ],
];
```

Add the Trait to your Model

```
