Background Image

Migrations & CRUDs

A LaraAdmin module is created by a migration that calls LAModule::generate(). It creates the table and the module's rows in la_modules and la_module_fields. To create a migration file for a Courses module:

php artisan la:migration courses
# or
php artisan la:migration create_courses_table

This writes database/migrations/YYYY_MM_DD_HHMMSS_create_courses_table.php:

use App\Models\LAModule;

LAModule::generate('Courses', 'courses', 'name', 'fa-book', [
    // the fields
]);

LAModule::generate() takes:

  1. Module name, plural: Courses
  2. Table name: courses
  3. View column: the column used as the record's title and link, e.g. name. It must be one of the fields, in lowercase.
  4. Font Awesome 4 icon: fa-book, fa-group...
  5. The fields, as an array

Field format

Each field is an array with these keys. Only colname, label and field_type are required:

[
    'colname' => 'name',          // column name: lowercase, words joined by _
    'label' => 'Name',            // label in forms and listings
    'field_type' => 'Name',       // one of the 31 field types below
    'unique' => false,            // values must be unique (max length becomes 250)
    'defaultvalue' => '',         // default value
    'minlength' => 0,             // min length / value / item count, 0 = none
    'maxlength' => 250,           // max length / value / item count, 0 = none
    'required' => true,           // required in the Add / Edit forms
    'listing_col' => true,        // shown in the listing table (default true)
    'popup_vals' => '',           // values: ['A', 'B'] or '@table_name'
    'comment' => '',              // optional note
]
KeyDefaultDescription
colnamerequiredDatabase column name. lowercase, words joined by underscores
labelrequiredLabel of the field, e.g. Name, Cost, Is Public
field_typerequiredOne of the field types
uniquefalseValues must be unique. Checked when a record is added.
defaultvalue''Default value for new records and the column
minlength / maxlength0Limits checked by the forms: length for text, value for numbers, number of items for Taginput, List and Checklist. For most text types maxlength is also the column length; 0 means no limit.
requiredfalseMandatory in the Add / Edit forms
listing_coltrueShow as a column of the listing table
popup_vals''For Dropdown, Radio, Multiselect, Taginput: a list ['Draft', 'Published'], or '@table_name' to choose records of another module's table (stored by id, with a foreign key)

A complete example:

LAModule::generate('Courses', 'courses', 'name', 'fa-book', [
    [
        'colname' => 'name',
        'label' => 'Name',
        'field_type' => 'Name',
        'unique' => true,
        'minlength' => 3,
        'maxlength' => 250,
        'required' => true,
    ], [
        'colname' => 'teacher_id',
        'label' => 'Teacher',
        'field_type' => 'Dropdown',
        'required' => true,
        'popup_vals' => '@employees',
    ], [
        'colname' => 'fees',
        'label' => 'Fees',
        'field_type' => 'Currency',
        'defaultvalue' => 0,
        'required' => true,
    ], [
        'colname' => 'description',
        'label' => 'Description',
        'field_type' => 'Textarea',
        'maxlength' => 1000,
        'listing_col' => false,
    ],
]);

Then create the table:

php artisan migrate

The 1.0 format, a plain list ["name", "Name", "Name", true, "", 5, 256, true] (column, label, type, unique, default, min, max, required, values), still works.

Migration from an existing module

To write the migration of a module that already exists in the database (for example one you built in Module Manager), add --generate. It updates the module's existing migration file, or creates one:

php artisan la:migration courses --generate

This is what Module Manager's Update Migration button does. Commit the file so that other installs create the same table.

Columns are listed as MySQL types. Text columns are NULLable unless noted. A field linked to @table stores the record's id.

Field typeForm inputColumnMin / MaxValues / default
AddressText areaVARCHAR(max), TEXT if max is 0Length
CheckboxToggleTINYINT(1)-Default true / false
ChecklistTo-do list with check boxesTEXT (JSON)Number of itemsItems can be ticked from the record page
ColorColor pickerVARCHAR(25)-Default #000
CurrencyNumberDOUBLE (1.1: DOUBLE(15,2))-Default 0.0
DateDate pickerDATE-A date such as 2026-01-31, or empty for none. The form shows dd/mm/yyyy.
DatetimeDate and time pickerTIMESTAMP-2026-01-31 14:20:00, or now() (column default CURRENT_TIMESTAMP)
DecimalNumberDECIMAL(15,2)-Default 0.0
DropdownSelectVARCHAR for a list, BIGINT UNSIGNED + foreign key for @table-List or @table. Quick add (+) adds a record to the linked module.
DurationDays / hours / minutesINT UNSIGNED (minutes)-Default 0
EmailEmailVARCHAR(max), VARCHAR(100) if max is 0Length
FileChoose / upload a fileBIGINT UNSIGNED + foreign key to uploads-Stores an upload id
FilesChoose / upload several filesVARCHAR(256) (JSON list of upload ids)-Default []
FloatNumberFLOATLength (checked as text)
HTMLRich text editorVARCHAR(10000)Length
ImageChoose / upload an imageBIGINT UNSIGNED + foreign key to uploads-Stores an upload id
IntegerNumberINT NOT NULLValueDefault 0
JSONTextJSON-Default {}
ListEditable list of linesTEXT (JSON)Number of items
LocationMap with address search, latitude and longitudeVARCHAR(30) (lat,lng)-Default 0.0,0.0. Needs the Google Maps script with your own API key: it is commented out in resources/views/la/layouts/partials/scripts.blade.php.
MobilePhone numberVARCHAR(max)Length
MultiselectMultiple selectVARCHAR(256) (JSON list)-List or @table. Default [] or ["A"] / [2, 3]
NameTextVARCHAR(max) NOT NULLLengthGood choice for the view column
PasswordPasswordVARCHAR(max)LengthStored hashed
RadioRadio buttonsVARCHAR for a list, INT UNSIGNED for @table-List or @table
StringTextVARCHAR(max)Length
TaginputTagsVARCHAR(1000) (JSON list)Number of tagsFree tags, or a list / @table to choose from
TextareaText areaVARCHAR(max), TEXT if max is 0Length
TextFieldTextVARCHAR(max)Length
TimeTime pickerVARCHAR(4) (HHMM, 24 h)-e.g. 0930, 1745
URLURLVARCHAR(max)Length

A text column with max 0 gets Laravel's default length (255) unless noted.

Examples of every type

One field of each type, ready to paste into LAModule::generate():

['colname' => 'address', 'label' => 'Address', 'field_type' => 'Address', 'maxlength' => 1000],
['colname' => 'restricted', 'label' => 'Restricted', 'field_type' => 'Checkbox', 'defaultvalue' => false],
['colname' => 'todo', 'label' => 'To Do', 'field_type' => 'Checklist', 'listing_col' => false],
['colname' => 'color', 'label' => 'Color', 'field_type' => 'Color', 'defaultvalue' => '#3c8dbc'],
['colname' => 'price', 'label' => 'Price', 'field_type' => 'Currency', 'defaultvalue' => 0, 'required' => true],
['colname' => 'date_release', 'label' => 'Date of Release', 'field_type' => 'Date'],
['colname' => 'time_started', 'label' => 'Start Time', 'field_type' => 'Datetime', 'defaultvalue' => 'now()'],
['colname' => 'weight', 'label' => 'Weight', 'field_type' => 'Decimal', 'defaultvalue' => 0],
['colname' => 'status', 'label' => 'Status', 'field_type' => 'Dropdown', 'defaultvalue' => 'Draft', 'popup_vals' => ['Draft', 'Published']],
['colname' => 'dept', 'label' => 'Department', 'field_type' => 'Dropdown', 'required' => true, 'popup_vals' => '@departments'],
['colname' => 'duration', 'label' => 'Duration', 'field_type' => 'Duration'],
['colname' => 'email', 'label' => 'Email', 'field_type' => 'Email', 'unique' => true, 'maxlength' => 250],
['colname' => 'brochure', 'label' => 'Brochure', 'field_type' => 'File'],
['colname' => 'attachments', 'label' => 'Attachments', 'field_type' => 'Files', 'listing_col' => false],
['colname' => 'rating', 'label' => 'Rating', 'field_type' => 'Float'],
['colname' => 'biography', 'label' => 'Biography', 'field_type' => 'HTML', 'listing_col' => false],
['colname' => 'profile_image', 'label' => 'Profile Image', 'field_type' => 'Image'],
['colname' => 'pages', 'label' => 'Pages', 'field_type' => 'Integer', 'minlength' => 0, 'maxlength' => 5000],
['colname' => 'settings', 'label' => 'Settings', 'field_type' => 'JSON', 'defaultvalue' => '{}', 'listing_col' => false],
['colname' => 'features', 'label' => 'Features', 'field_type' => 'List', 'maxlength' => 10],
['colname' => 'location', 'label' => 'Location', 'field_type' => 'Location'],
['colname' => 'mobile', 'label' => 'Mobile', 'field_type' => 'Mobile', 'maxlength' => 20],
['colname' => 'media_type', 'label' => 'Media Type', 'field_type' => 'Multiselect', 'defaultvalue' => ['Print'], 'popup_vals' => ['Print', 'Audiobook', 'E-book']],
['colname' => 'name', 'label' => 'Name', 'field_type' => 'Name', 'minlength' => 3, 'maxlength' => 250, 'required' => true],
['colname' => 'password', 'label' => 'Password', 'field_type' => 'Password', 'minlength' => 6, 'maxlength' => 250, 'listing_col' => false],
['colname' => 'gender', 'label' => 'Gender', 'field_type' => 'Radio', 'defaultvalue' => 'Female', 'popup_vals' => ['Female', 'Male', 'Other']],
['colname' => 'author', 'label' => 'Author', 'field_type' => 'String', 'maxlength' => 250],
['colname' => 'genre', 'label' => 'Genre', 'field_type' => 'Taginput', 'defaultvalue' => ['Fantasy'], 'maxlength' => 5],
['colname' => 'description', 'label' => 'Description', 'field_type' => 'Textarea', 'maxlength' => 1000],
['colname' => 'short_intro', 'label' => 'Short Introduction', 'field_type' => 'TextField', 'maxlength' => 250],
['colname' => 'opens_at', 'label' => 'Opens At', 'field_type' => 'Time', 'defaultvalue' => '0930'],
['colname' => 'website', 'label' => 'Website', 'field_type' => 'URL', 'maxlength' => 250],

Once the migration has run, one command generates the CRUD:

php artisan la:crud Courses

It generates the same files as Module Manager's Generate Migration + CRUD button:

  1. Controller app/Http/Controllers/LA/CoursesController.php
  2. Model app/Models/Course.php
  3. Observer app/Observers/CourseObserver.php, registered in LAProvider
  4. Views resources/views/la/courses/ (index, edit, show)
  5. Language file resources/lang/en/la_course.php
  6. Routes appended to routes/admin.php, and log types in config/laraadmin.php
  7. A sidebar menu item

The generated code is built from the templates in app/Stubs/. To change what every new module looks like, edit those; see Customizing.


What's Next ?