PHPackages                             devslane/generator - 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. devslane/generator

ActiveLibrary

devslane/generator
==================

Generate files in laravel

3643PHPCI failing

Since Jan 6Pushed 5y agoCompare

[ Source](https://github.com/Hemant11111/mcs-generator)[ Packagist](https://packagist.org/packages/devslane/generator)[ RSS](/packages/devslane-generator/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

### Introduction

[](#introduction)

This package focuses on reusability of code and provides support to laravel framework. The package has been developed with the aim to save developement time by providing developers very useful commands and helping in project structure setup.

### Installation

[](#installation)

To install this package you will need:

- PHP 7.2 +
- Laravel 5.8 +

Run the following from the terminal to install the package:

```
composer require devslane/generator
```

```
composer require devslane/generator --dev

```

---

*Install via composer* - edit your `composer.json` to require the package.

```
"require": {
    "devslane/generator": "dev-master"
}

```

Then run composer update in your terminal to pull it in.

---

### Configuration:

[](#configuration)

- Add the service provider to the providers array in your app.php config as follows:

```
Devslane\Generator\Providers\GeneratorServiceProvider::class,

```

- Publish the configuration file with the following Artisan command:

```
php artisan vendor:publish --provider="Devslane\Generator\Providers\GeneratorServiceProvider"

```

---

#### Helper commands.

[](#helper-commands)

- [`php artisan generate:migration`](#generate-migration)
- [`php artisan generate:model`](#generate-model)
- [`php artisan generate:contract`](#generate-contract)
- [`php artisan generate:request`](#generate-request)
- [`php artisan generate:exception`](#generate-exception)
- [`php artisan generate:service`](#generate-service)
- [`php artisan generate:transformer`](#generate-transformer)
- [`php artisan generate:controller`](#generate-controller)
- [`php artisan generate:factory`](#generate-factory)
- [`php artisan generate:seeder`](#generate-seeder)
- [`php artisan generate:all`](#generate-all)

---

### Generate Migration

[](#generate-migration)

Supports to create migration with columns of provided datatype, with constraints or column with foreign key:

```
php artisan generate:migration {table : Table name} {--columns= : Columns}

```

```
php artisan generate:migration users --columns=first_name:string,last_name:string,age:integer

```

```
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
			$table->string('first_name');
			$table->string('last_name');
			$table->integer('age');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}
```

Add unique and nullable constraints:

```
php artisan generate:migration table1 --columns=name:string/nullable
php artisan generate:migration table2 --columns=name:string/unique

```

Foreign Key constraint on a column can also be provided with the command as:

```
php artisan create:migration posts --columns=post:string,user_id:fk=users
```

---

### Generate Model

[](#generate-model)

The command generates the Eloquent Models for the tables provided with the command.

```
php artisan generate:model {tables*}

```

```
php artisan generate:model users

```

Generates: **User.php**

```
namespace App;
use Devslane\Generator\Models\BaseModel;

/**
 * Class User
 *
 * @package App
 */
class User extends BaseModel
{

}
```

### Generate Contract

[](#generate-contract)

This command generates the required Contracts interfaces which are then implemented by the Request classes.

*generate:contract {tables\*}*

```
php artisan generate:contract users
```

Generates the following:

- Create**User**Contract
- List**User**Contract
- Update**User**Contract

These Interfaces contain the most essential functions. The *Delete**User**Contract* is not created. It can be manually added if requirement arises.

```
namespace App\Services\Contract;

interface ListUserContract
{

}
```

```
namespace App\Services\Contract;

interface CreateUserContract
{
    public function getFirstName();

    public function getLastName();

    public function getAge();

    public function getCreatedAt();

    public function getUpdatedAt();
}
```

```
namespace App\Services\Contract;

interface UpdateUserContract
{
	public function getFirstName();

	public function hasFirstName();

	public function getLastName();

	public function hasLastName();

	public function getAge();

	public function hasAge();

	public function getCreatedAt();

	public function hasCreatedAt();

	public function getUpdatedAt();

	public function hasUpdatedAt();
}
```

### Generate Request

[](#generate-request)

This command generates all the CRUD operation's Request classes. These classes implement the corresponding Contracts generated by *generate:contract {tables\*}*.

*generate:request {tables\*}*

```
php artisan generate:request users
```

Running this for the very first time generates *ListRequest* Class which is then extended by all the ListRequest Classes.

- ListRequest

```
namespace App\Api\V1\Requests;

use Devslane\Generator\Requests\BaseRequest;

/**
 * Class ListTesttableRequest
 * @package App\Api\V1\Requests
 */
class ListRequest extends BaseRequest
{
    const LIMIT        = 'limit';
    const ORDER        = 'order';
    const ORDER_BY     = 'order_by';
    const SEARCH_QUERY = 'search_query';

    public function getLimit() {
        return $this->input(self::LIMIT);
    }

    public function getOrder() {
        return $this->input(self::ORDER);
    }

    public function getOrderBy() {
        return $this->input(self::ORDER_BY);
    }

    public function getSearchQuery() {
        return $this->input(self::SEARCH_QUERY);
    }
}
```

Generates the following:

- Create**User**Request
- List**User**Request
- Update**User**Request

These classes implement the corresponding contracts generated by the previous command and also contain the implementation of the functions of the Contracts. The *Delete**User**Request* is not created. It can be manually added if requirement arises.

```
namespace App\Api\V1\Requests;

use App\Services\Contract\ListUserContract as Contract;

/**
 * Class ListUserRequest
 * @package App\Api\V1\Requests
 */
class ListUserRequest extends ListRequest implements Contract
{

}
```

```
namespace App\Api\V1\Requests;

use App\Services\Contract\CreateUserContract as Contract;

/**
 * Class CreateUserRequest
 * @package App\Api\V1\Requests
 */
class CreateUserRequest extends ListRequest implements Contract
{
    const FIRST_NAME = 'first_name';
    const LAST_NAME  = 'last_name';
    const AGE        = 'age';
    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';

    public function getFirstName() {
        return $this->input(self::FIRST_NAME);
    }

    public function getLastName() {
        return $this->input(self::LAST_NAME);
    }

    public function getAge() {
        return $this->input(self::AGE);
    }

    public function getCreatedAt() {
        return $this->input(self::CREATED_AT);
    }

    public function getUpdatedAt() {
        return $this->input(self::UPDATED_AT);
    }
}
```

```
namespace App\Api\V1\Requests;

use App\Services\Contract\UpdateUserContract as Contract;

/**
 * Class UpdateUserRequest
 * @package App\Api\V1\Requests
 */
class UpdateUserRequest extends ListRequest implements Contract
{
    const FIRST_NAME = 'first_name';
    const LAST_NAME  = 'last_name';
    const AGE        = 'age';
    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';

    public function getFirstName() {
        return $this->input(self::FIRST_NAME);
    }

    public function hasFirstName() {
        return $this->has(self::FIRST_NAME);
    }

    public function getLastName() {
        return $this->input(self::LAST_NAME);
    }

    public function hasLastName() {
        return $this->has(self::LAST_NAME);
    }

    public function getAge() {
        return $this->input(self::AGE);
    }

    public function hasAge() {
        return $this->has(self::AGE);
    }

    public function getCreatedAt() {
        return $this->input(self::CREATED_AT);
    }

    public function hasCreatedAt() {
        return $this->has(self::CREATED_AT);
    }

    public function getUpdatedAt() {
        return $this->input(self::UPDATED_AT);
    }

    public function hasUpdatedAt() {
        return $this->has(self::UPDATED_AT);
    }
}
```

### Generate Exception

[](#generate-exception)

This generates the *NotFoundExceptions* related to the tables provided with the command.

*generate:exception {tables\*}*

```
php artisan generate:exception users
```

Generates: **User**NotFoundException

```
namespace App\Api\V1\Exceptions;

/**
 * Class UserNotFoundException
 * @package App\Api\V1\Exceptions
 */
class UserNotFoundException extends \Symfony\Component\HttpKernel\Exception\HttpException
{
    const ERROR_MESSAGE = 'User not Found';
    const ERROR_CODE = 404;

    public function __construct($statusCode = 422)
    {
        parent::__construct($statusCode, self::ERROR_MESSAGE, null, array(), self::ERROR_CODE);
    }
}
```

### Generate Service

[](#generate-service)

This command generates Services for the given tables containing CRUD operations in the service. These services are used by the Controllers to execute CRUD operations on the Database.

*generate:service {tables\*}*

```
php artisan generate:service users
```

Generates: **User**Service

```
namespace App\Services;

use App\Api\V1\Exceptions\UserNotFoundException;
use App\User;
use App\Services\Contract\CreateUserContract;
use App\Services\Contract\UpdateUserContract;

/**
 * Class UserService
 * @package App\Services
 */
class UserService
{

    /**
     * @param $userId
     * @return User
     */
    public static function find($userId) {
        $user = User::find($userId);
        if (!$user) {
            throw new UserNotFoundException();
        }
        return $user;
    }

    /**
     * @return User[]|\Illuminate\Database\Eloquent\Collection
     */
    public function index() {
        return User::all();
    }

    /**
     * @param $userId
     * @return User
     */
    public function show($userId) {
        return self::find($userId);
    }

    /**
     * @param CreateUserContract $contract
     * @return User
     */
    public function store(CreateUserContract $contract) {
        $user = new User();
                $user->first_name = $contract->getFirstName();
        $user->last_name = $contract->getLastName();
        $user->age = $contract->getAge();

        $user->save();
        return $user;
    }

    /**
     * @param $userId
     * @param UpdateUserContract $contract
     * @return User
     */
    public function update($userId, UpdateUserContract $contract) {
        $user = self::find($userId);
                if ($contract->hasFirstName()) {
            $user->first_name = $contract->getFirstName();
        }
        if ($contract->hasLastName()) {
            $user->last_name = $contract->getLastName();
        }
        if ($contract->hasAge()) {
            $user->age = $contract->getAge();
        }

        $user->save();
        return $user;
    }

    /**
     * @param $userId
     */
    public function delete($userId) {
        $user = $this->find($userId);
        try {
            $user->delete();
        } catch (\Exception $e) {
        }
    }
}
```

### Generate Transformer

[](#generate-transformer)

Generates the Transformer for the response that will be generated after any CRUD operation, the Controllers exploit these Transformers to transform the response in the desired format.

By default the transformers contain all the columns of the table.

*generate:transformer {tables\*}*

```
php artisan generate:transformer users
```

Generates: **User**Transformer

```
namespace App\Transformers;

use App\User;
use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract
{

    public function transform(User $user){
        return [
			'id' => $user->id,
			'first_name' => $user->first_name,
			'last_name' => $user->last_name,
			'age' => (int)$user->age,
			'created_at' => $user->created_at,
			'updated_at' => $user->updated_at,
		];
    }
}
```

### Generate Controller

[](#generate-controller)

Generates a controller having fundamental CRUD functions, which the routes point to. These controller classes use the generated Services for the database operations.

Generated transformers are used by the controller before sending the response.

*generate:controller {tables\*}*

```
php artisan generate:controller users videos posts
```

Generates: **User**Controller

```
namespace App\Api\V1\Controllers;

use App\Transformers\UserTransformer;
use App\Api\V1\Requests\CreateUserRequest;
use App\Api\V1\Requests\UpdateUserRequest;
use App\Services\UserService;
use Devslane\Generator\Controllers\BaseController;

/**
 * Class UserController
 * @package App\Api\V1\Controllers
 *
 * @property-read UserService $userService
 */
class UserController extends BaseController
{
    protected $userService;

    public function __construct() {
        $this->userService = new UserService();
    }

    /**
     * @param UserService $service
     * @return \Dingo\Api\Http\Response
     */
    public function index(UserService $service) {
        $users = $service->index();
        return $this->response->collection($users, new UserTransformer());
    }

    /**
     * @param $id
     * @param UserService $service
     * @return \Dingo\Api\Http\Response
     */
    public function show($id, UserService $service) {
        $user = $service->show($id);
        return $this->response->item($user, new UserTransformer());
    }

    /**
     * @param CreateUserRequest $request
     * @param UserService $service
     * @return \Dingo\Api\Http\Response
     */
    public function store(CreateUserRequest $request, UserService $service) {
        $user = $service->store($request);
        return $this->response->item($user, new UserTransformer());
    }

    /**
     * @param UpdateUserRequest $request
     * @param $id
     * @param UserService $service
     * @return \Dingo\Api\Http\Response
     */
    public function update(UpdateUserRequest $request, $id, UserService $service) {
        $user = $service->update($id, $request);
        return $this->response->item($user, new UserTransformer());
    }

    /**
     * @param $id
     * @param UserService $service
     */
    public function destroy($id, UserService $service) {
        $service->delete($id);
    }
}
```

### Generate Factory

[](#generate-factory)

Generates the Factory for the tables provided.

*generate:factory {tables\*}*

```
php artisan generate:facotory users posts videos
```

Generates: **User**Factory

```
use App\User;
use Faker\Generator as Faker;

$factory->define(User::class, function (Faker $faker) {
    return [
		'first_name'=>$faker->firstName,
		'last_name'=>$faker->lastName,
		'age'=>$faker->numberBetween(0,100),
		'created_at'=>$faker->dateTime,
		'updated_at'=>$faker->dateTime,
	];
});
```

### Generate Seeder

[](#generate-seeder)

Generates the Database seeder for the tables. These seeders use the corresponding factories generated by *generate:factory {tables\*}*. By default the number of rows are 10 for the seeders and can be modified by changing it in the *mcs-helper.php* in config.

```
return [
.
.
    'seeder'      => [
        'row_count'     => 10,
        'path'          => 'database/seeds',
        'overwrite'     => true,
        'exclude_table' => [
            'password_resets', 'migrations',
        ],
    ]
.
.
]
```

*generate:seeder {tables\*}*

```
php artisan generate:seeder users videos posts
```

Generates: **User**Seeder

```
use App\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Config;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */

    public function run() {
        $size = (integer)Config::get('mcs-helper.seeder.row_count');
        factory(User::class, $size)->create();
    }
}
```

### Generate All

[](#generate-all)

*generate:all {tables\*}*

```
php artisan generate:all users videos posts
```

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity34

Early-stage or recently created project

 Bus Factor1

Top contributor holds 96% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/46d81efb9f3d4c3b1da29484b8bf79478b72444d188a0c84024e4c1236134bb4?d=identicon)[devslane](/maintainers/devslane)

---

Top Contributors

[![jarvisphere](https://avatars.githubusercontent.com/u/15008930?v=4)](https://github.com/jarvisphere "jarvisphere (24 commits)")[![Shrish2784](https://avatars.githubusercontent.com/u/30043202?v=4)](https://github.com/Shrish2784 "Shrish2784 (1 commits)")

### Embed Badge

![Health badge](/badges/devslane-generator/health.svg)

```
[![Health](https://phpackages.com/badges/devslane-generator/health.svg)](https://phpackages.com/packages/devslane-generator)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
