PHPackages                             drawmyattention/laravel-make-resource - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. drawmyattention/laravel-make-resource

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

drawmyattention/laravel-make-resource
=====================================

Generate a model, migration, controller, routes and factory

v1.0.0(10y ago)9242123[2 issues](https://github.com/amochohan/laravel-make-resource/issues)1MITPHPPHP &gt;=5.5.9

Since May 19Pushed 9y ago7 watchersCompare

[ Source](https://github.com/amochohan/laravel-make-resource)[ Packagist](https://packagist.org/packages/drawmyattention/laravel-make-resource)[ RSS](/packages/drawmyattention-laravel-make-resource/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (6)Versions (2)Used By (1)

Laravel artisan make:resource command
=====================================

[](#laravel-artisan-makeresource-command)

[![Build Status](https://camo.githubusercontent.com/5913dc615a1a84ba80de3890ae0dee1d5263dd291d0d6f229d0cd8fd30193394/68747470733a2f2f7472617669732d63692e6f72672f616d6f63686f68616e2f6c61726176656c2d6d616b652d7265736f757263652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/amochohan/laravel-make-resource) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/6ff2582e02983c00ee05ffea62b65dd5911f0fbf67e3ad8692119cae52da9163/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f647261776d79617474656e74696f6e2f6c61726176656c2d6d616b652d7265736f757263652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/drawmyattention/laravel-make-resource/?branch=master) [![codecov](https://camo.githubusercontent.com/1fdbbc80a9243d626b7946e412fc01f2980dc191c78f4c4cd12fc2e9e76055ac/68747470733a2f2f636f6465636f762e696f2f67682f616d6f63686f68616e2f6c61726176656c2d6d616b652d7265736f757263652f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/amochohan/laravel-make-resource)[![License](https://camo.githubusercontent.com/30597ff9a350144f03bffdd9183e16468e0b3ca1193e1d08591d992622738d55/687474703a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](http://www.opensource.org/licenses/MIT)

This package adds the `php artisan make:resource command`, allowing you to:

> Generate a model, set its attributes, create a migration, controller, routes and model factory in a single easy to use command.

This package serves as a way of very quickly getting an idea off the ground, reducing the time you need to spend setting up various parts of your application so that you can concentrate on the complexity.

Why use this package?
---------------------

[](#why-use-this-package)

When starting a new project, typically we'll begin by creating a new model, and then going into that model and defining its fillable attributes. Next, we'll set up a migration, and again define which columns the table should hold.

Next we generate a controller, and add methods for `index`, `show`, `edit`, `update`, `create`, and `store` and finally open up the routes.php file to set up endpoints that relate to the methods in the controller.

If you practice test-driven development, or write automated tests, you'll then need to create a model factory and again define the same attributes.

I found myself going through the same long winded process time and time again, so decided to build a single command which can:

- Create a model
- Set its fillable and hidden attributes
- Generate a migration, with column definitions based on the model
- Build a restful controller, with the model imported
- Add the corresponding restful routes namespaced under the model name
- A model factory, with the same attributes and sensible faker dummy data

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

[](#installation)

Install MakeResource through Composer.

```
"require": {
    "drawmyattention/laravel-make-resource": "~1.0"
}

```

Next, update your `config/app.php` to add the included service provider to your `providers` array:

```
'providers' => [
    // other providers
    DrawMyAttention\ResourceGenerator\ResourceGeneratorServiceProvider::class,
];

```

And you're good to go.

Using the generator
-------------------

[](#using-the-generator)

From the command line, run:

```
php artisan make:resource ModelName "attributes"

```

For the simplest example, let's create a new `Animal` resource:

```
php artisan make:resource Animal

```

This will create the following:

- app\\Animal.php
- app\\Http\\Controllers\\AnimalController.php
- database\\migrations\\2016\_05\_19\_090000\_create\_animals\_table.php

as well as appending to:

- app\\Http\\routes.php
- database\\factories\\ModelFactory.php

Defining model attributes
-------------------------

[](#defining-model-attributes)

It's also possible to provide a pipe-separated list of attributes for the model. For example:

```
php artisan make:resource Animal "name:string,fillable,100,index|legs:integer,fillable,unsigned|colour|owner:hidden"

```

The convention to use when passing arguments is, simply a pipe separated list:

> \[attribute name\]:\[comma separated properties\]

The order of the properties is *not* important.

If you specify either `fillable` or `hidden`, the property will be set accordingly. If neither is provided, the property is not added to either.

Take a look at the `colour` and `owner` properties from the example. No data type was provided, so these are automatically cast to a string type.

Example Animal model
--------------------

[](#example-animal-model)

```
