Generating Code
Resource Creation:
After installation, developers can generate resources for their models using the `php artisan flex:scaffold` command. This automates the creation of the necessary files and configuration for managing that specific resource within the FlexAdmin admin panel.
Example:
php artisan flex.scaffold:controller YourModel
php artisan flex:model YourModel
php artisan flex:scaffold YourModel
php artisan flex:rollback YourModel scaffold
When you run a command, it gives the following instructions,
Specify fields for the model (skip id & timestamp fields, we will add it automatically)
(Read docs carefully to specify field inputs)
Enter "exit" to finish
Here, you have to insert fields for your models, except the id & timestamps (created_at & updated_at) fields. That will be automatically added by the generator. When you are done with inserting fields. Type “exit”.
When you run any command, it asks for two things:
- Field Inputs
- Validations
Field Inputs
Now, let’s get started with specifying the field.
Field input format is divided into four parts,
name <space> db_type <space> html_type <space> options
Here is what each part means.
name
name of the field (snake_case recommended). for e.g.,
- title
- user_id
- body
- status
db_type
database type of the field. e.g.
- string –
$table->string('field_name')
- string,25 –
$table->string('field_name', 25)
- text –
$table->text('field_name')
- For Enum, enum,Sun,Mon,Tue –
$table->enum('field_name', ['Sun', 'Mon', 'Tue'])
- integer,false,true –
$table->integer('field_name',false,true)
- string:unique –
$table->string('field_name')->unique()
- For foreign keys
- foreignId:constrained – $table->foreignId(‘field_name’)->constrained()
- integer:unsigned:foreign,table_name,id –
$table->foreign('field_name')->references('id')->on('table_name')
- integer:unsigned:foreign,table_name,id,cascade –
$table->foreign('field_name')->references('id')->on('table_name')->onUpdate('cascade')->onDelete('cascade')
html_type
HTML type of field for forms. e.g.
- text
- textarea
- password
Here is the full guide for HTML field inputs.
options
Options to prevent the field from being searchable, fillable, displayed in form & index
Here are all the options by which you can prevent it, these all fields should be passed by comma comma-separated string.
e.g. s,f,if,ii
- s - specify to make field non-searchable
- f - specify to make field non-fillable
- if - to skip field from being asked in form
- ii - to skip field from being displayed in index view
- iv - to skip field from being displayed in all views
so here are some examples, of how field inputs can be passed together
title string text
body text textarea s,ii
email string:unique email
writer_id integer:unsigned:foreign,writers,id text s
Validations
In the second field, you can specify validations for fields from any available validations of Laravel.
e.g.
- required
- min:5
- numeric
You can pass the same string as the Laravel doc suggests. e.g. required|unique:posts|max:255
The generator also supports various other commands to generate files individually like generating only a model, repository controller, etc. You can find a full doc here.