CRUD Generator Configuration

After you generate a CRUD config file using the crud:config command, you'll want to modify it. All CRUD config files are located in the config/crud folder of your app.

Each CRUD config file contains arrays for paths, icon, and attributes.

Paths

These are the paths used by the generator when creating your CRUD scaffolding.

Stubs

This is the path to the stub template files. It will use the package files by default (recommended), but you can also create your own stub template files by publishing the defaults:

php artisan vendor:publish --provider="Kjjdion\LaravelAdminPanel\LapServiceProvider" --tag="crud_stubs"

This will create a new directory in resources/stubs/crud/default. You can rename the default folder to whatever you want, then make sure to use this new path in your CRUD config file if you want to use it for generating.

You will notice a number of replacment strings throughout the stub template files e.g. {model_variables}. When the generator runs, it replaces all of these strings with their actual values.

Other Paths

All other options simply point to where the generator will create files and insert content. For example, controller is the path where the new controller file will be created. Another example is menu, this is where the generator will insert the new menu item after the first </li> (usually the Dashboard link).

Icon

This is simply the FontAwesome 5 icon class to use for the menu item e.g. fa-cogs.

Attributes

This is where most of your attention is needed. From this array, you can set all of the attributes the model will have. The default CRUD stubs already contain scaffolding for id, created_at, and updated_at, so you won't need to enter these attributes if using the default package stubs.

Each attribute can be defined in the following format:

'title' => [
    'migrations' => [
        'string:title|unique',
    ],
    'validations' => [
        'create' => 'required|unique:vehicles',
        'update' => 'required|unique:vehicles,title,{$vehicle->id}',
    ],
    'datatable' => [
        'title' => 'Title',
        'data' => 'title',
    ],
    'input' => [
        'type' => 'text',
    ],
],

Notice how the array key is the actual model attribute itself, and the values are its options. Each attribute option can be omitted.

The following options are available for each attribute you specify.

primary

Specifies if this attribute should be used as the primary label for the model. This is useful for things like the activity log, where the message contains a searchable phrase for the model.

'primary' => true,

migrations

Specifies the migration methods for the attribute in the format of method|method:param|method:param,param.

'migrations' => [
    'string:title|unique',
],

The example above would turn into $table->string('title')->unique();.

casts

Specifies the $casts mutator for the attribute.

'casts' => 'array',

relationship

Specifies the relationship for the attribute in the format of 'model_method' => 'method|method:param|method:param,param.

'relationship' => [
    'user' => 'belongsTo:App\User',
],

user_timezone

Specifies if the attribute should be converted into the users timezone via Carbon.

'user_timezone' => true,

validation

Specifies the validation rules for the attribute in the same format used by the Laravel Validator. Note that the key for each is which controller method the rules are for.

'validations' => [
    'create' => 'required|unique:vehicles',
    'update' => 'required|unique:vehicles,name,{$vehicle->id}',
],

Also note the use of {$vehicle->id} here. In this example, the CRUD config file would be named Vehicle.php, and used to generate CRUD for a Vehicle model. Therefore, we can use the $vehicle variable, which is an instance of Vehicle injected into the controller method.

datatable

Specifies the values used in order to generate the datatable column in the model index table.

'datatable' => [
    'title' => 'Title',
    'data' => 'title',
    'orderable' => false,
],

Please see the Laravel Datatables Column Docs for information on available columns.

You can also set the data for a relationship by using dot annotation .e.g 'data' => 'user.name'.

input

Specifies the form input to use for the attribute.

'input' => [
    'type' => 'select',
    'options' => ['Red', 'Green', 'Blue'],
],

The type can be checkbox, radio, file, select, text, textarea, or any HTML5 input type e.g. date.

You can also specify options using code methods, associative arrays, or sequential arrays.

options using code methods:

'options' => [
    'app:App\User|orderBy:name|get' => [
        'id' => 'name',
    ],
],

Notice the key in the format of method|method:param|method:param,param. Also, the input options will defined as 'value' => 'label', which represents the attribute for the object returned e.g. $user->id => $user->name.

options using associative arrays:

'options' => [
    'auto' => 'Automatic Transmission',
    '4x4' => '4x4 Drivetrain',
],

options using sequential arrays:

'options' => ['Red', 'Green', 'Blue'],

The same conventions for options apply to checkbox, radio, and select. However, if you want a checkbox with a single option, you should specify the value and label for said checkbox:

'input' => [
    'type' => 'checkbox',
    'value' => true,
    'label' => 'This vehicle is financed',
],

Complete Example

Here is a complete example (config/crud/Vehicle.php) of some configuration values you could use:

<?php

return [

    // paths & files used for generating
    'paths' => [
        'stubs' => 'vendor/kjjdion/laravel-admin-panel/resources/stubs/crud/default',
        'controller' => 'app/Http/Controllers/Admin',
        'model' => 'app',
        'migrations' => 'database/migrations',
        'views' => 'resources/views/admin',
        'menu' => 'resources/views/vendor/lap/layouts/menu.blade.php',
        'routes' => 'routes/web.php',
    ],

    // menu icon (fontawesome class)
    'icon' => 'fa-car',

    // model attributes
    'attributes' => [

        // relationship
        'user_id' => [
            'migrations' => [
                'integer:user_id',
            ],
            'relationship' => [
                'user' => 'belongsTo:App\User',
            ],
            'validations' => [
                'create' => 'required|exists:users,id',
                'update' => 'required|exists:users,id',
            ],
            'datatable' => [
                'title' => 'User',
                'data' => 'user.name',
            ],
            'input' => [
                'type' => 'select',
                'options' => [
                    'app:App\User|orderBy:name|get' => [
                        'id' => 'name',
                    ],
                ],
            ],
        ],

        // text
        'title' => [
            'primary' => true,
            'migrations' => [
                'string:title|unique',
            ],
            'validations' => [
                'create' => 'required|unique:vehicles',
                'update' => 'required|unique:vehicles,title,{$vehicle->id}',
            ],
            'datatable' => [
                'title' => 'Title',
                'data' => 'title',
            ],
            'input' => [
                'type' => 'text',
            ],
        ],

        // file
        'main_photo' => [
            'migrations' => [
                'string:main_photo|nullable',
            ],
            'validations' => [
                'create' => 'nullable|image',
                'update' => 'nullable|image',
            ],
            'input' => [
                'type' => 'file',
            ],
        ],

        // multiple files
        'photos' => [
            'migrations' => [
                'string:photos|nullable',
            ],
            'input' => [
                'type' => 'file',
                'multiple' => true,
            ],
        ],

        // textarea
        'description' => [
            'migrations' => [
                'text:description',
            ],
            'validations' => [
                'create' => 'required|min:10',
                'update' => 'required|min:10',
            ],
            'input' => [
                'type' => 'textarea',
            ],
        ],

        // select sequential
        'company' => [
            'migrations' => [
                'string:company',
            ],
            'validations' => [
                'create' => 'required|in:Chevrolet,Dodge,Ford',
                'update' => 'required|in:Chevrolet,Dodge,Ford',
            ],
            'datatable' => [
                'title' => 'Company',
                'data' => 'company',
            ],
            'input' => [
                'type' => 'select',
                'options' => ['Chevrolet', 'Dodge', 'Ford'],
            ],
        ],

        // select associative
        'color' => [
            'migrations' => [
                'string:color|nullable',
            ],
            'validations' => [
                'create' => 'nullable|in:#ff0000,#00ff00,#0000ff',
                'update' => 'nullable|in:#ff0000,#00ff00,#0000ff',
            ],
            'datatable' => [
                'title' => 'Color',
                'data' => 'color',
                'searchable' => false,
                'orderable' => false,
            ],
            'input' => [
                'type' => 'select',
                'options' => [
                    '#ff0000' => 'Red',
                    '#00ff00' => 'Green',
                    '#0000ff' => 'Blue',
                ],
            ],
        ],

        // radio relationship
        'first_user' => [
            'migrations' => [
                'integer:first_user|nullable',
            ],
            'input' => [
                'type' => 'radio',
                'options' => [
                    'app:App\User|orderBy:name|get' => [
                        'id' => 'name',
                    ],
                ],
            ],
        ],

        // radio sequential
        'type' => [
            'migrations' => [
                'string:type|nullable',
            ],
            'input' => [
                'type' => 'radio',
                'options' => ['Car', 'Truck'],
            ],
        ],

        // radio associative
        'tires' => [
            'migrations' => [
                'string:tires|nullable',
            ],
            'input' => [
                'type' => 'radio',
                'options' => [
                    'all' => 'All Season Tires',
                    'winter' => 'Winter Tires',
                ],
            ],
        ],

        // single checkbox
        'financed' => [
            'migrations' => [
                'boolean:financed|nullable',
            ],
            'input' => [
                'type' => 'checkbox',
                'value' => true,
                'label' => 'This vehicle is financed',
            ],
        ],

        // multiple checkboxes relationship
        'other_users' => [
            'migrations' => [
                'text:other_users|nullable',
            ],
            'casts' => 'array',
            'input' => [
                'type' => 'checkbox',
                'options' => [
                    'app:App\User|orderBy:name|get' => [
                        'id' => 'name',
                    ],
                ],
            ],
        ],

        // multiple checkboxes sequential
        'features' => [
            'migrations' => [
                'text:features|nullable',
            ],
            'casts' => 'array',
            'input' => [
                'type' => 'checkbox',
                'options' => ['Heated Seats', 'Power Sunroof'],
            ],
        ],

        // multiple checkboxes associative
        'engine' => [
            'migrations' => [
                'text:engine|nullable',
            ],
            'casts' => 'array',
            'input' => [
                'type' => 'checkbox',
                'options' => [
                    'auto' => 'Automatic Transmission',
                    '4x4' => '4x4 Drivetrain',
                ],
            ],
        ],

        // date
        'sold_at' => [
            'migrations' => [
                'timestamp:sold_at|nullable|index',
            ],
            'user_timezone' => true,
            'datatable' => [
                'title' => 'Sold At',
                'data' => 'sold_at',
            ],
        ],

    ],

];