Generate CRUD Command

gen:crud Command

Basic Usage

The CRUD Generator provides a single command, gen:crud, that you can use to generate CRUD resources for a given model. Here's the basic syntax:

php artisan gen:crud {ModelName} --fields="fieldName:dataType fieldName:dataType"

Replace {ModelName} with the name of the model you want to generate CRUD resources for, and specify the fields for that model using the --fields option.

For example, to generate CRUD resources for a Post model with fields name (string), description (text), and count (integer), you would run:

php artisan gen:crud Post --fields="name:str description:text count:int"

Interactive Questions

During the CRUD generation process, the command-line tool will ask you a few interactive questions to customize the generated code according to your preferences. These questions are:

The first question asks if you want to add the generate files in a specific folder:

Do you want to add controllers in a specific folder? (yes/no) [no]:

if you answer no, it will generate crud the default way

If you answer yes, it will prompt you to enter the folder name where you want to place the controllers:

Enter the Folder Name:

Make sure Folder Name is in Capital Case ex:Admin not admin

question example

Adding Soft-Deletes in CRUD

To add this functionality simply append --softDelete in gen command

php artisan gen:crud Post --fields="name:str description:text" --softDelete

Adding Relationships

To add this functionality simply add --relations="your code here" in gen command

Example: To generate Profile CRUD with relations (hasOne: Account, hasMany: Blogs and belongTo: User)

php artisan gen:crud Profile --fields="name:str user_id:fid" --relations="haso:account hasm:blogs belt:user"
Relation Name
Shorthand For

haso

hasOne

hasm

hasMany

belt

belongsTo

belm

belongsToMany

Note: you can use hasMany, belongsTo etc directly in --relations command if you feel comfortable and it currently only supports these 4 common relations type.

Adding Select and Enum Select

php artisan gen:crud Profile --fields="name:str user_type:enum_select:name=UserType user_id:select:model=User,name"

This will generate a CRUD for the Profile model with:

  • name (string field)

  • user_type (enum select field with options from UserType enum)

  • user_id (select field with options from User model, displaying the name attribute)

Enum Select Field

  • Syntax: fieldName:enum_select:name=EnumName

  • Example: user_type:enum_select:name=UserType

  • Creates a dropdown with options from the specified Enum

  • The Enum should be located in the app/Enum/ folder

  • The dropdown will display the values (not keys) from the Enum

Model Select Field

  • Syntax: fieldName:select:model=ModelName,displayAttribute

  • Example: user_id:select:model=User,name

  • Creates a dropdown with options from the specified Model

  • The dropdown will use the model's ID as the value and the specified attribute (name in this case) as the display text

  • Enum Location: Ensure your Enum classes are in the app/Enum/ folder.

  • Enum Structure: The Enum should have a key-value structure. The values will be displayed in the frontend select field.

  • Model Select: For model-based selects, the ID is automatically used as the key.

Delete CRUD Command

php artisan crud:api ModelName

When generating an CRUD using the CRUD Generator package, pay attention to the following:

  1. CRUD Generation Folder

    • If you want to generate the CRUD inside a specific folder, you'll be prompted with a "yes/no" question.

    • If you answer "yes", you'll be asked to provide the folder name in the next command.

  2. Deleting the CRUD

    • Be cautious when deleting the generated CRUD, as it will also delete the associated Model.

    • If the Model is being used by an existing CRUD, deleting the CRUD will cause issues with the CRUD functionality.

delete crud with folder

Last updated