PHP Classes

File: readme.md

Recommend this page to a friend!
  Classes of TJ Webb   Laravel CRUD Generator   readme.md   Download  
File: readme.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: Laravel CRUD Generator
Generate MVC classes for any Laravel model
Author: By
Last change:
Date: 4 years ago
Size: 7,020 bytes
 

Contents

Class file image Download

Laravel Crud

Laravel Crud is a package for automatically adding CRUD (Create, Read, Update, Delete) views, web controllers and API controllers for any model as rapidly as possible.

Prototyping

Laravel Crud let's you quickly create all of the views and controllers you need for the full CRUD operation set (create, read, update, delete), including an index with just a configuration file. Laravel Crud also creates and registers routes for you automatically, giving you both web and API endpoints for your models with as little as one line in an array.

The intent of Laravel Crud is for rapid prototyping. All you need for your complete CRUD operation set is a model and corresponding database table. The properties and their types are read from the database and the appropriate UI controlls are rendered in the web UIs.

Installation

Install via composer.

composer require webbtj/crud

Usage

Once installed you'll want to publish a config file to edit.

php artisan vendor:publish

When prompted, select crud-config. This will create config/crud.php where you can define the models you want crud functionality generated for.

Configuration

You can list any models you want crud functionality for in the config/crud.php configuration file. This file returns an array, each element in the array can be either a string naming a model, or an array with additional configuration options such as which fields are read only, excluded from certain views, and even validation. When specifying the model name, you can include the full namespace of the model (App\Employee) or simply the name of the class itself (Employee). The model name is also case-insensitive.

Example configuration file:

return [
    'models' => [
        // Add your model class names here (full namespace)
        // Exmaple: "App\\Employee"

        "App\\SmallChild", // a string that will create all routing and views
                           // for the model with all defaults.

        [
            'model' => "App\\FastCar", // specify the model with extra configs
            'index' => [
                'exclude' => ['created_at'], // don't show this property in the
                                             // index view
            ],
            'store' => [
                'validation' => [ // run this validation on the "store" method
                    'year' => 'required'
                ],
            ],
        ],

        [
            'model' => "App\\Employee",
            'show' => [
                'exclude' => ['sample_text', 'sample_string'],
            ],
            'edit' => [
                'exclude' => ['sample_longText'],
                'readonly' => ['sample_char'], // in the edit interface, show
                                               // this property but make it
                                               // read only
            ],
            'create' => [
                'exclude' => ['sample_text'],
                'include' => ['updated_at'], // include this property in the
                                             // create interface (it's normally
                                             // not included)
                'readonly' => ['sample_integer'],
            ],
            'index' => [
                'include' => [
                    'id', 'first_name', 'last_name', 'email', 'sample_json',
                    'sample_jsonb', 'sample_enum', 'sample_set', 'enabled'
                ],
            ],
            'update' => [
                'validation' => [
                    'first_name' => 'required',
                    'last_name' => 'required',
                ],
            ]
        ]
    ]
];

You can specify include, exclude, and readonly arrays of properties for each of the four standard views, show, edit, create, and index. You can also specify include, exclude, and validation arrays for each of the two standard methods, store and update. If you're specifying additional options in an array format, you must include the model definition as well.

Defaults

While you have complete control over what fields are displayed and can be edited, there are defaults that the package will fall back to when you do not provide specifics. By default...

  • no validation is applied to any properties
  • `id`, `created_at`, and `updated_at` are not updateable via requests
  • each view will display all properties

In all views except index, you will exclude fields you don't want displayed. If you want to customize the index view you will need to include each property.

Production

But reading schemas for every interaction with a model, reading and parsing all of these inclusion, exclusion, read-only, and validation rules from a config, these are all pretty expensive operations and not really suited for production apps. That's why there's an artisan command to commit views and controllers to _your_ codebase for better performance and further development control.

php artisan crud:publish

The crud:publish artisan command will create a directory for the model in your views directory with index, show, create, and edit views. It will also create a web controller in your Http/Controllers directory and an API controller in your Http/Controllers/Api directory. Finally it will provide recommended code for adding the routes to your web.php and api.php routes files, and recommend that you now remove the publish model(s) from your crud.php config file.

Of course you can customize and limit this publish with options. Provide --model= to speficfy the model you wish to publish. Omitting this option will publish all models. This option allows for multiple values simply by specifying it more than once (Example: `php artisan crud:publish --model=Employee --model=Car).

You can also specify which elements you want published by specifying --type=. Like --model= this can be repeated to specify multiple types to publish. The valid types are as follows:

  • `controller` - publishes the web controller
  • `api.controller` - publishes the API controller
  • `views` - publishes all views
  • `view.index` - publishes just the index view
  • `view.show` - publishes just the show view
  • `view.create` - publishes just the create view
  • `view.edit` - publishes just the edit view

Some notes on these options. They are case insensitive; all punctuation is stripped out (so api.controller, api-controller, and apicontroller all work); singular vs plural doesn't matter (English only); and the order of words for the specific views doesn't matter (view.index or index.view).

Roadmap

  • Unit testing
  • Beta release/release to Packagist.

Contributing

Contributions are always welcome on GitHub. Please open issues before submitting PRs and do tag the issue in your commit messages/PR description. Also, please adhere to PSR-2 as much as possible.