Create Admin Panel with Laravel Backpack Part 2

In today article “Create Admin Panel with Laravel Backpack Part 2” we will talk about how to create a quick and powerful admin panel. For this article we gonna take a real database example to make a real life admin panel for a book database.

In the first part we talk about how to setup basic installation and configuration of Laravel Backpack. Now we will create the basic crud for a book database.

I found a book database example on this website. I choose to use it because it’s a real database that you can use if you need it. They have some sample free so we can integrate in our admin and even create an importer tools.

To continue with that article you need to follow the part 1 “Create Admin Panel with Laravel Backpack Part 2” on this following link

What we are going to do?

In this part we will mostly focus on the database operations and routes. We will create the required controllers and models.

Our database schema will look like the following capture. Don’t worry we will make all of this quickly with backpack integrated tools.

Database schema for backpack laravel admin application
Database schema Laravel Backpack

CRUD

With Laravel Backpack creating crud is easy and fast but first what’s a CRUD.

CRUD is an acronym for Create Read Update and Delete, so Crud is a section an the admin that let you do operation on table. We gonna create different crud. First we will create crud for the Author table.

Authors

Let’s make the CRUD for authors table. First of all we need to create migration.

php artisan make:migration:schema create_authors_table --model=0 --schema="title:string,slug:string,biography:text:nullable"
php artisan migrate

So in this command we create a table with name authors, the option –model=0 mean do not create a model for this migration. The argument schema=”” is where you have to put your table content with attribute. As you can see in our example the biography field can be null.

You can find all the documentation of this package on GitHub by following this link.

After creating the migration now we will create model, request and controller for that migration. You havel to simply run this command. Be sure to use singular name of the table or migration name.

php artisan backpack:crud author

Now we got out model, request and controller created. We need to add some routes to able to access to them. You should notice the new files we created in your application structure like the following picture.

Laravel Backpack file structure
File structure

Execute the following command that will add a line to the file routes/backpack/custom.php. This is the file where we manage the admin route.

php artisan backpack:base:add-custom-route "CRUD::resource('author', 'AuthorCrudController');"

Of course you can add them manually. The structure of the route is like the laravel resource routes. The first parameter is the crud name singular, the second one is the name of the matching controller. This Controller located in app/Http/Controller/Admin. By the way your routes file should look like this.

<?php
 
// --------------------------
// Custom Backpack Routes
// --------------------------
// This route file is loaded automatically by Backpack\Base.
// Routes you generate using Backpack\Generators will be placed here.
 
Route::group([
    'prefix'     => config('backpack.base.route_prefix', 'admin'),
    'middleware' => ['web', config('backpack.base.middleware_key', 'admin')],
    'namespace'  => 'App\Http\Controllers\Admin',
], function () { // custom admin routes
    CRUD::resource('author', 'AuthorCrudController');
}); // this should be the absolute last line of this file

The same process to add an idem to the sidebar menu. The creators of backpack make it easy with a command. But you still can do it manually by going to the right file. The sidebar file is located in resources/views/vendor/backpack/base/inc/sidebar_content.blade.php.

php artisan backpack:base:add-sidebar-content "<li><a href='{{ backpack_url('author') }}'><i class='fa fa-user'></i> <span>Authors</span></a></li>"

So basically we add a element in the list with a custom route and using font-awesome for the icon. Now start your application php artisan serve go to your admin page http://localhost:8000/admin then login you should have something similar to this.

Author admin

Books

To create the books crud we gonna follow the same step

# Step 1 Create migrate
php artisan make:migration:schema create_books_table --model=0 --schema="title:string,author:string,author_id:integer,author_bio:string,title_slug:string,isbn13:string:unique,isbn10:string:unique,price:double,format:string,publisher:string,pubdate:date,edition:string,lexile:string,subjects:string, pages:integer,dimensions:string,overview:text,synopsis:text,excerpt:string,toc:string,editorial_reviews:string"
#Step 2 migrate
php artisan migrate
 
#Step 3 Create model, request and controller
php artisan backpack:crud book
 
#Step 4 adding route
php artisan backpack:base:add-custom-route "CRUD::resource('book', 'BookCrudController');"
 
#Step 5 adding item to the menu
php artisan backpack:base:add-sidebar-content "<li><a href='{{ backpack_url('book') }}'><i class='fa fa-book'></i> <span>Books</span></a></li>"

After executing the following command we can take a look at the result here.

Book Crud

Subjects

This table contains the subject for the book like the categories or sub categories

# Step 1 Create migrate
php artisan make:migration:schema create_subjects_table --model=0 --schema="title:string,slug:string"
#Step 2 migrate
php artisan migrate
 
#Step 3 Create model, request and controller
php artisan backpack:crud subject
 
#Step 4 adding route
php artisan backpack:base:add-custom-route "CRUD::resource('subject', 'SubjectCrudController');"
 
#Step 5 adding item to the menu
php artisan backpack:base:add-sidebar-content "<li><a href='{{ backpack_url('subject') }}'><i class='fa fa-tag'></i> <span>Subjects</span></a></li>"

Categories

Note in this article i use categories as name for the table names sub_subject in the database example for clarity. The following command is to create the categories

# Step 1 Create migrate
php artisan make:migration:schema create_categories_table --model=0 --schema="title:string,slug:string"
#Step 2 migrate
php artisan migrate
 
#Step 3 Create model, request and controller
php artisan backpack:crud category
 
#Step 4 adding route
php artisan backpack:base:add-custom-route "CRUD::resource('category', 'CategoryCrudController');"
 
#Step 5 adding item to the menu
php artisan backpack:base:add-sidebar-content "<li><a href='{{ backpack_url('category') }}'><i class='fa fa-list-alt'></i> <span>Categories</span></a></li>"

Sub-Categories

The original table in this sample was sub_sub_subject but i renamed it to subcategories. You can you another name if you want .

# Step 1 Create migrate
php artisan make:migration:schema create_subcategories_table --model=0 --schema="title:string,category_id:integer,slug:string, count:integer:default(0)"
#Step 2 migrate
php artisan migrate
 
#Step 3 Create model, request and controller
php artisan backpack:crud subcategory
 
#Step 4 adding route
php artisan backpack:base:add-custom-route "CRUD::resource('subcategory', 'SubcategoryCrudController');"
 
#Step 5 adding item to the menu
php artisan backpack:base:add-sidebar-content "<li><a href='{{ backpack_url('subcategory') }}'><i class='fa fa-list-alt'></i> <span>Sub-Categories</span></a></li>"

Pivot table

Now we need a pivot table to link the book with the subjects, categories and sub categories. I follow the same database schema as the example and of course can be improved.

# Step 1 Create migrate
php artisan make:migration:pivot categories books

To follow the same logic a the example database we gonna add another column named subcategory_id in the pivot table. Make sure your last migration look that.

class CreateBookCategoryPivotTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('book_category', function (Blueprint $table) {
            $table->integer('book_id')->unsigned()->index();
            $table->foreign('book_id')->references('id')->on('books')->onDelete('cascade');
            $table->integer('category_id')->unsigned()->index();
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->integer('subcategory_id')->unsigned()->index();
            $table->foreign('subcategory_id')->references('id')->on('subcategories')->onDelete('cascade');
            $table->primary(['book_id', 'category_id']);
        });
    }
 
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('book_category');
    }
}

Now we have the basic of and admin we can do the CRUD operations but we need some more advances options. We will customize the CRUD operations do some validations and update the laravel models.

You can find the code in our GitHub repository by following this link .

Conclusion

In the next article we will add and customize validations and crud operations. In this article “Create Admin Panel with Laravel Backpack Part 2” we created database and basic crud. If you have any issue while following this article feel free to leave a comment to get some help.

You may also like...

3 Responses

  1. triumphatly 509 says:

    nou te pale yo!

  1. July 28, 2019

    […] Create admin panel with laravel backpack part 2 (Setup database and create models and controllers) […]

  2. August 7, 2019

    […] Create admin panel with laravel backpack part 2 (Setup database and create models and controllers) […]

Leave a Reply

Your email address will not be published. Required fields are marked *