Laravel 5.7 CRUD Tutorial Example using Craftable

In this article I’m gonna show you how easily you can build a simple CRUD admin interface for you Laravel 5.7 application. We will use Craftable – our open-source toolkit for building administration interfaces.

In this article we will cover the process when you want to start a fresh new project. But don’t worry, if you already have an existing Laravel 5.7 application, you can use Craftable too. Just follow the instructions Add Craftable to existing project and then follow with step 5.

Note that Craftable supports also Laravel 5.6 and Laravel 5.5, if you prefer to use olders versions.

Step 0: Before you start

Make sure you have latest PHP installed (currently PHP 7.2) and you are meeting the standard Laravel 5.7 requirements. Make sure you have a web server running.

Also make sure you have either MySQL 5.7+ or PostgreSQL 9.5+ database installed and running.

Step 1: Install Craftable installer

First, we need to install the Craftable installer, so we can install fresh new copies of a Craftable anytime, just like in Laravel:

composer global require brackets/craftable-installer

Step 2: Create empty database

In order to install the default Craftable, we woudl definitely need an empty database. You can choose between MySQL or PostgreSQL. You can create one using your favourite database client or via console. For example, for PostgreSQL you can use:

createuser my_blog_user

This creates the user my_blog_user – it always is a good practice to keep things separated.

createdb -O my_blog_user my_blog

This creates the my_blog database.

Step 3: Create a new project

Let’s say we are about to create a new Blog:

craftable new my_blog

This will create a new folder named my_blog and install latest Laravel and latest Craftable in it. Installer will print the password for the default administrator account. Save this password, we are going to need it soon.

Step 4: Compile assets

Finally we need to compile all the assets using standard:

npm install && npm run dev

Once installed, navigate your browser to /admin/login. You should be able to see a login screen similar to this one:

I think it’s time to look around inside Craftable. The default credentials are:

E-mail: administrator@brackets.sk
Password: use the password you have saved in step 3

After successful login you should see a main menu with two options: Manage access and Translations. These are two basic modules Craftable ships with by default. Let’s add some new.

Step 5: Create a migration

When building a blog, next step should be a new migration file. We would probably need some articles table with some title, slug, body and probably some published_at attribute. Let’s do it:

php artisan make:migration create_articles_table

and place the following content to the up method:

Schema::create('articles', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->string('slug')->unique();
    $table->text('body')->nullable();
    $table->date('published_at')->nullable();
    $table->timestamps();
});

Don’t forget to run the migration using:

php artisan migrate

Step 6: Generate the CRUD

Now all you have to do is generate the CRUD using one of Craftable’s generator commands:

php artisan admin:generate articles

This will generate everything. Just hit the refresh in the browser and see – whole CRUD for the articles is there.

Generator has created:
– model
– controller
– requests
– resources (views, assets, translations)

You can now edit any generated code to better meet your needs.

Conclusion

Craftable helps you quickly scaffold the basic CRUD skelet of a new module. It saves you time with the repetitive part of the job, so you can better focus on all the specific details of each module and adjust them to your needs.

Subscribe for newsletter

Get the freshest news and keep up with social media world.

Stay informed through the email

Your email is safe with us, read our terms.

Stay informed through the email

Stay informed through the email

Your email is safe with us, read our terms.