Please see the full documentation here.
This package adds a crud:generate command to Artisan in your Laravel project. With this command you can generate CRUD (Create, Read, Update, Delete) for your db tables instantly!
The generated CRUD includes a Controller, Model, Policy, routes, validations, and 4 view files (index, create, edit, show). It also supports relations between tables, so you can generate nested CRUDs, for example: generating CRUD for comments inside a blog post.
The output is fully customizable and supports both Bootstrap 5 and Tailwind CSS.
Laravel-crud can generate nested CRUDs. For example, you can generate CRUD for customers > tickets > replies by simply running:
$ php artisan crud:generate customers.tickets.replies
(Automatically generated index page with responsive table layout)
The generated views are fully customizable and template based. For example, you can change the look of the index page by using different templates like table, cards or chat (included in the package).
$ php artisan crud:generate customers.tickets.replies -i cards
(Card layout)
$ php artisan crud:generate customers.tickets.replies -i chat
(Chat layout)
Laravel CRUD generator can automatically generate the FORM for creating and editing records. It can also generate the validation rules for the form.
(Example of generated fields)
The package is still in alpha
You should only use it with new projects. If you are using it in an existing project, make sure to back up (or commit) any project changes before running the command.
The aim of the CRUD generator is to save you time by generating common boilerplate CRUD code for any database table instantly.
But this code is not intended to be used as-is. You should always customize the generated code to fit your needs and manually review the generated files before deploying it to production.
Via composer:
$ composer require san-kumar/laravel-crud --dev
Important!
Make sure to create the db tables and run migrations before starting the crud generator.
After installing the package you should see a
new crud:generate
command in your Artisan commands list.
To generate the Controller, Model, Policy, routes, etc use the following command:
# Create CRUD for authors table
$ php artisan crud:generate authors
# Create CRUD for the authors > posts table
$ php artisan crud:generate authors.posts
# Create CRUD for the authors > posts > comments table
$ php artisan crud:generate authors.posts.comments
By default, the generated routes are placed in
the ./routes/crud/
directory. To include the generated
routes just open your routes/web.php
file and add the
following line at the end of the file:
/* inside routes/web.php */
\San\Crud\Crud::routes();
This will glob
all the generated route files and include
them in the routes/web.php
file for you.
Alternatively, you can copy-paste the generated routes from
the ./routes/crud/
directory to your routes/web.php
file. This way you make any changes to the generated route
code as per your needs.
The crud:generate
command will generate the following
files and place them in the appropriate directories:
app/Http/Controllers/[Name].php
app/Model/[Name].php
app/Policies/[Name]Policy.php
routes/crud/[name].php
resources/views/[name]/(index|edit|create|show).blade.php
By default, the generated code will not overwrite any
existing files. If you want to overwrite the existing files,
you can use the --force
option.
You can choose between Bootstrap 5 and Tailwind CSS or you can use your own CSS framework. By default, Bootstrap 5 is used when nothing is specified.
# Create CRUD for authors table using Tailwind CSS
$ php artisan crud:generate authors --css=tailwind
# Shortcut for Tailwind CSS
$ php artisan crud:generate authors -t
# Create CRUD for authors table using layout 'layouts/admin.blade.php'
$ php artisan crud:generate authors --layout=admin
$ php artisan crud:generate authors --section=mysection
You can add a route prefix for the generated CRUD by passing
a --prefix
option.
# Create CRUD for authors table using Tailwind CSS
$ php artisan crud:generate authors --prefix=admin
This will change the route from /authors
to
/admin/authors
in all the generated files.
You can choose between 3 ready-made index templates: table, cards and chat. By default, the table template is used.
# Create CRUD for authors table using cards template
$ php artisan crud:generate authors -i cards
# Create CRUD for authors table using chat template
$ php artisan crud:generate authors -i chat
Please see the index page here for screenshots.
In some cases, you may want to use a single form for both
creating and editing a resource. You can do that by passing
--merge-forms
option.
# Create CRUD for authors table using a single form
$ php artisan crud:generate authors --merge-forms
This will generate a single view
file called
create-edit.blade.php
and update the controller code
accordingly so that it can be used for both creating and
editing a resource.
By default, the generated views use layouts/app.blade.php
for layout and content
for section name. You can change
these default names by passing your own layout and section
names.
In addition to generating simple CRUD for a single table, you can also generate CRUD for tables related to each other.
For example, let's say we have the following tables:
To generate CRUD for these tables you can use the dot
notation to specify the parent tables like this:
authors.posts.comments
.
# Create CRUD for authors table
$ php artisan crud:generate authors
# Create CRUD for posts table
$ php artisan crud:generate authors.posts
# Create CRUD for comments table
$ php artisan crud:generate authors.posts.comments
This will generate CRUD files for the authors
table, so
you can view, create, edit and delete authors using the
/authors
route.
Then for each author, you can view, create, edit and delete
posts using the /authors/{author}/posts
route.
And for each post, you can view, create, edit and delete
comments using the /authors/{author}/posts/{post}/comments
route.
On the other hand, if you want to generate CRUD for the
posts
table without nesting it under the authors
route,
you can do this:
# Create CRUD for posts table
$ php artisan crud:generate posts
# Create CRUD for comments table
$ php artisan crud:generate posts.comments
So now you can view, create, edit and delete all posts
regardless of the author using the /posts
route and then
for each post, you can view, create, edit and delete
comments using the /posts/{post}/comments
route.
Laravel CRUD generator uses the default Laravel
authentication
package laravel/ui
for handling user authentication. The CRUD generator will
automatically add authentication to routes (using the
auth
middleware) and generate Policy classes for Models.
user_id
field in tableIf you generate CRUD for any table that has a user_id
field, then the generated CRUD will automatically generate a
Policy file for the Model and add the
authorizeResource
method to the Resource Controller.
user_id
in parent tablesIf you generate CRUD for a table that has a user_id
field
in the parent tables (i.e. authors.posts.comments
has a
user_id
field in the authors
table), then the generated
CRUD will automatically generate a Policy file for
the Authors
Model and add it to your Resource Controller
in the appropriate places.
This way access to posts
and comments
will be restricted
to the owner of the authors
table only.
Laravel CRUD generator can automatically generate the FORM for creating and editing records. It can also generate the validation rules for the form.
The form fields are generated based on the database table columns. The following column types are supported:
(Example of generated fields)
string
(input type text)text
(textarea)integer
(input type number)enum
(select)boolean
(radio buttons)relation
(select with hydrated options)It can also infer the field type from the column names. For
example, if the column name is email
, the field type will
be email
.
By default, all non-nullable fields are required. Columns
with a unique
index are also validated to be unique in the
table.
Some additional validation rules are also added based on the
column name. For example, if the column name is email
, the
validation rule will be email
.
If the table has any foreign key columns, the generator will automatically add a select field for that column. The options for the select field will be hydrated from the related table.
For example, if your table has a country_id
column and if
the country
table exists: the generator will add a select
field for country (as shown in the image above) and the
options for that select field will be hydrated from
the countries
table (if the country table has a user_id
column, the options will be filtered by the current user).
You do need to add relation fields manually or create any
foreign key columns in the database. The relation is
inferred from the column name only. Any column name that
matches the pattern *_id
are considered relation.
For example, if the column name is country_id
, the
generator will look for a countries
table and add if that
exists (and if the current user has permission to access
that table) it will add a select field for that column and
hydrate the options from the countries
table.
Laravel provides a simple way to implement soft deletes on your Eloquent models. Soft deletes allow you to keep a record of a model's deletion without actually deleting the model from your database. This allows you to restore the model at a later time if needed.
Laravel CRUD generator handles tables with soft deletes automatically and generates the appropriate code for you to restore or purge deleted records.
(Handling soft deletes)
To show deleted records in the list view, just append
?trashed=1
to the URL (this link isn't added to the
navigation menu by default, so you have to add it manually).
A very simple search is added to the list view by default. It searches the first text column of the table and filters the results.
This generated search code is just a placeholder, and you should customize it as per your own needs.
(Inbuilt search)
To modify the search algorithm just edit the index
method
of the Resource controller for that route.
At the heart of Laravel CRUD generator are the templates files that are used to generate the CRUD files.
The templates files included with this package are stored in
the ./vendor/san-kumar/laravel-crud/src/template
directory.
It contains all the stub files that are used to generate the Controller, Model, Policy, routes, etc.
You can customize the templates by copying the template into a new directory and then modifying the template files.
To make this simple Laravel CRUD generator provides an artisan command that will copy the template files into a new directory for you.
# Copy the template files into a new directory
$ php artisan crud:template newname
This will copy the template files into a new directory
./templates/newname/
directory.
It you want to specify a different directory to copy the
template files into you can use the --directory
or
-d
option.
# Copy the template files into a new directory
$ php artisan crud:template newname -d etc/templates
This will copy the template files into a new directory
./etc/templates/newname/
directory.
To use your own templates you need to specify the template
directory in the config/crud.php
file.
// config/crud.php
return [
'template_dir' => 'etc/templates/newname',
];
Alternatively you can specify the template directory using
the --template
or -t
option while generating the CRUD.
# Generate the CRUD using the new template
$ php artisan crud:generate Post --template etc/templates/newname
You will need to modify the template files in the new directory to customize the generated files.
You will find multiple variables that are used in the template files that are replaced with the actual values when the files are generated.
To see the full list of variables that are used in the template files check out the source code for GenerateCrud.php
Laravel CRUD generator doesn't seem to work with Laravel jetstream just yet. If you are using jetstream, you can still use the package, but you will have to manually modify some generated code to fix the layout and authentication issues.
A very simple search is added to the list view by default. It searches the first text column of the table and filters the results. But this generated search code is just a rudimentary placeholder, and you should customize the search algorithm as per your own needs.
While the package tries to be as flexible as possible, it
still generates some opinionated code. For example, the
generated CRUD uses Laravel's Policy
classes for handling
authorization. But you may prefer to use something else
like spatie/laravel-permission
, which can be done but
requires you
to create your own templates.
You should only use it with new projects. If you are using it in an existing project, make sure to back up (or commit) any project changes before running the command.
Laravel CRUD Generator allows you to remove the generated
files using the artisan crud:remove
command.
$ php artisan crud:remove customers.tickets.replies
This will remove all the files generated by
the crud:generate
for this table.
If you want to keep the generated files, you can use the
--backup
option to move the files into a Zip archive
instead.
$ php artisan crud:remove customers.tickets.replies --backup=backup.zip
This too will remove the files from the disk but will also create a Zip archive of all the removed files too.
The software is licensed using a MIT License. It means you can do whatever you want with it (including using it for commercial purposes freely), as long as you include the original copyright and license notice in any copy of the software/source.
Found any bugs? Report them on the issue tracker.
Want to contribute? Fork the project on GitHub and send a pull request.
Like the project? Star it on GitHub.