Commands
CRUD Generator

CRUD Generator command

CRUD Generator allows you to easily generate CRUD for existing table. It will generate:

  • Model class
  • Controller class
  • Request classes
  • Routes
  • Vue files for listing and form
  • Typescript definitons
  • Export class

Usage

We support two ways of generating CRUD. Using command options or using wizard mode which will guide you through the process.

To use wizard mode, you can either run the command without any arguments like this:

php artisan craftable-pro:generate-crud

Or you can run the command with table name with -w or --wizard option like so:

php artisan craftable-pro:generate-crud {table_name} -w

Alternatively you can simply run the command with table name and it will generate CRUD based on the options you provided without any questions. If you do not provide any options, the generator will do its best to guess what you need and will prefill most of the options with default values.

For example:

php artisan craftable-pro:generate-crud {table_name} --listing-columns=id,title,description --sortable-columns=id,title --form-columns=title,description,price
💡
Parameter table_name must be an existing table

Options

  • --w|wizard -> Wizard mode asks for most common options interactively

  • --listing-columns -> List and select columns for index page

  • --sortable-columns -> List of columns which listing can be sorted by

  • --form-columns -> List and select columns for form page

  • --publishable-column -> Name of column which should be used for publishable feature

  • --translatable-columns -> List of columns which should be translatable

  • --media-collections -> Names of media collections which should be registered for model

  • --image-collections -> Names of media collections which should have conversions generated as well

  • --add-relation -> Definition of relation on model using following syntax:

    <relationType>(<relatedModel>-><modelAccessor>,<?foreign_key>,<?owner_key>)
    (ie: `belongsTo(Author->title)` or `belongsTo(Author->title,author_id,id)`)
  • --with-export -> Whether listing should have export function or not

  • --run-migration -> Whether to run permission migration after generating CRUD or not

  • --without-routes -> Do not generate routes

  • --without-sidebar -> Do not append new module into the sidebar

  • --dry-run -> Display all information without actually generating anything';

Wizard mode

When using wizard mode, generator will ask you a question about each feature you haven't provided directly with command option. You can combine this mode with explicitly defined options using their name from the list above.

Bellow you will find all the options explained in more detail.

Listing columns

This option allows you to select columns which will be displayed in the listing. You can select multiple columns by separating them with comma.

Which columns should be visible on the listing page? [id,title,slug,perex,published_at,author_id]
id .................................................................... 0
title ................................................................. 1
slug .................................................................. 2
perex ................................................................. 3
published_at .......................................................... 4
author_id ............................................................. 5
>id,title,slug,published_at

Sortable columns

This option allows you to select columns which will be sortable in the listing. You can only choose from columns selected in the previous step. You can select multiple columns by separating them with comma.

Which columns should be sortable in listing? [id,title,slug,perex,published_at,author_id]
id .................................................................... 0
title ................................................................. 1
slug .................................................................. 2
perex ................................................................. 3
published_at .......................................................... 4
author_id ............................................................. 5
>id,title,published_at

Form columns

This option allows you to select columns which will be visible in the Vue form. You can select multiple columns by separating them with comma.

Which columns should be visible on form page? [title,slug,perex]
title ................................................................. 0
slug .................................................................. 1
perex ................................................................. 2
published_at .......................................................... 3
author_id ............................................................. 4
>title,perex,published_at,author_id

Translatable columns

This option allows you to select columns which will be translatable. You can only choose from those, which have defined json or jsonb type id database. You can select multiple columns by separating them with comma.

Which columns should be translatable (only JSON columns)? [title]
title ................................................................. 0
slug .................................................................. 1
perex ................................................................. 2
>title,slug,perex

Translatable columns

This option allows you to select column which will be used for publishable feature. This will generate nice button in listing, with which you can quickly schedule or unpublish certain record. You can only choose from date or datetime columns.

Which column should be used for publishable feature? [published_at]
published_at .......................................................... 0
>published_at

Media collections

This option allows you to write names of media collection you want to register for your model. You can register multiple collections by separating them with comma.

Write name of media collections you want to register. Separate them by comma or leave empty if you don't want any media collections.
>hero,files

Image collections

This option allows you to select which of the media collection you want to use as an image collections. You can select multiple collections by separating them with comma.

Select which of those media collections should be of type image?
hero .................................................................. 0
files ................................................................. 1
>hero

Relations

This option allows you to define relationships on your model. Since the DX is different in wizard mode and option command, please take a look at the examples bellow for more information.

This option consist of multiple steps, so please take a look at the example bellow to get the idea about which question you can expect. Bear in mind that some of them might change depending on your choices.

Do you want to add a relation? [no]
yes .................................................................. 0
no ................................................................... 1
>yes
What is the type of the relation? [belongsTo]
belongsTo ............................................................ 0
belongsToMany ........................................................ 1
>belongsTo
What is the name of the related model?
Author ............................................................... 0
Comment .............................................................. 1
..custom ............................................................. 2
>Author
What is the foreign key column in Post model? [author_id]
id ................................................................... 0
author_id ............................................................ 1
>author_id
What is the referenced primary key column in Author model? [id]
id ................................................................... 0
>id
Which attribute should represent the Author model in Post module (typically 'name' or 'title')? [first_name]
first_name ........................................................... 0
last_name ............................................................ 0
>first_name

At the end you can decide if you want to define another relation or not.

Do you want to add a relation? [no]
yes .................................................................. 0
no ................................................................... 1
>no

Export

This option allows you to define if you want to generate export of your records to excel.

Do you want to use export feature? [no]
yes .................................................................. 0
no ................................................................... 1
>yes

Dry run

This option allows you to see outocome of your choices without actually generating or changing any files. You have to specify it when running the command itself:

php artisan craftable-pro:generate-crud -w --dry-run
Last updated on February 3, 2023