PHPackages                             sarkis-sh/laravel-spray - 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. sarkis-sh/laravel-spray

ActiveTool[API Development](/categories/api)

sarkis-sh/laravel-spray
=======================

A Laravel code generation tool to accelerate project setup and streamline API development.

1.0.2(2y ago)013MITPHPPHP &gt;=7.4

Since Jan 3Pushed 2y ago1 watchersCompare

[ Source](https://github.com/sarkis-sh/laravel-spray)[ Packagist](https://packagist.org/packages/sarkis-sh/laravel-spray)[ RSS](/packages/sarkis-sh-laravel-spray/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (2)Versions (5)Used By (0)

Spray - Laravel Code Generation Tool
====================================

[](#spray---laravel-code-generation-tool)

[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](LICENSE)

Spray is a powerful tool for Laravel developers that focuses on code generation. It allows developers to concentrate on the business logic of their projects without worrying about routine API and code tasks.

Prerequisites
-------------

[](#prerequisites)

Before using Spray, make sure you have the following:

1. A ready MySQL database for your project.
2. An empty Laravel project with an initialized database connection in the `.env` file.
3. The following path exists in your environment variables:
    `C:\Users\{UserName}\AppData\Roaming\Composer\vendor\bin`

Getting Started
---------------

[](#getting-started)

To use Spray, follow these steps:

1. Open terminal inside your project's root directory and Run the command `s-spray`.
2. You can also run the command `s-spray` outside any project and select the project you want or add a new project.
3. Follow the instructions.

Spray will check if the directory is a Laravel directory. Afterward, it will prompt you to enter your X-API-KEY for your Postman account and the collection ID for your project. You can skip this step and add these fields later. This step is necessary to create and keep your APIs up-to-date in your Postman collection.

You can also customize the body type of the Store and Bulk Store APIs that are generated in the Postman collection. By default, the body type is set to RAW JSON. However, you have the option to change it to the following options:

- Urlencoded
- Formdata
- Raw JSON

This allows you to choose the body type that best suits your API requirements and preferences.

Code Generation
---------------

[](#code-generation)

Spray will copy important files to your project the first time you use it. These files are crucial for getting your project ready to use and adding new APIs. The files include:

- Trait files for resource, response, and file management (such as file uploads).
- Language files in Arabic and English.
- Important middlewares like CORS, Authentication, and SetLocale. Please make sure to register these middlewares in your `App\Http\Kernel.php` file if you want to use them. After registering the middlewares, you can then use them in your `App\Providers\RouteServiceProvider.php` or with any specific route as per your requirements.
- A generic exception handler.
- Generic Controller, Service, Model, FormRequest, and Resource classes. These classes form the main structure of our projects, following a Service-Oriented Architecture (SOA), and handle all routine APIs, including Get All, Find By Id, Store, Bulk Store, Update, Delete, and Bulk Delete.

**Note: You can find these files inside the `resources` directory in Spray project.**

Spray will also model the database structure and save it for **monitoring** purposes. It will display a list of all existing tables and allow you to select any table or select all tables with options to generate APIs for them.

The code generation process includes:

- Generating a Controller that extends the generic Controller.
- Generating a Service that extends the generic Service.
- Generating a Model that extends the generic Model, including the fillable array, timestamps properties, and any hasMany or belongsTo relations.
- Generating a Request that extends the generic Request with validators for your selected APIs, filled with rules based on the table structure.
- Generating a Resource that extends the generic Resource with a filled array based on the table columns.
- Generating a Factory filled with faker for each column, using the column name for built-in functions or the column type for custom faking.
- Generating routes based on your selected APIs and appending them to the `routes/api.php` file.
- Generating the APIs in your Postman collection, allowing you to conveniently test and interact with them.
- Filling the language files with validation attributes based on your unique database table columns, translated into English, and leaving them empty in the Arabic file.

After the code generation process is complete, all the generated APIs for your selected tables are ready to use without any further editing. Simply run `php artisan optimize` to finalize the generation process.

Monitoring and Updating
-----------------------

[](#monitoring-and-updating)

Spray continuously monitors any changes in your database, such as adding, updating, or deleting columns or adding new tables. It will highlight the updated tables in the console to alert you to the changes.

Spray can also update all previously generated code, including form requests, resources, model fillable and timestamps properties, factories, Postman collection and language files. This feature saves you time by automatically updating the generated code to reflect any changes in your database.

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

[](#requirements)

- PHP version 7.4 or higher.

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

[](#installation)

You can install Spray using Composer:

```
composer global require sarkis-sh/laravel-spray
```

Alternatively, you can clone the repository and run the file inside the `bin` folder.

Usage
-----

[](#usage)

Spray is a console-based tool. Simply follow the instructions provided by Spray in the terminal.

Example
-------

[](#example)

### Users table

[](#users-table)

NameTypeNullidunsigned bigintNonamevarchar(100)Noageunsigned intYesemailvarchar(51)Nopasswordvarchar(255)Nocreated\_attimestampYesupdated\_attimestampYes### Generated Classes

[](#generated-classes)

#### UserController

[](#usercontroller)

```
