Create Admin Panel with Laravel Backpack Part 4

In today article “Create Admin Panel with Laravel Backpack Part 4” we will talk about how to create a quick and powerful admin panel. For this article we gonna add filter to our CRUD and make it easier to add data and more advanced features.

In the first part we talk about how to setup basic installation and configuration of Laravel Backpack. In the second part we setup the database and create models, controller. We also created routes and menu. In the third we created the basic crud for a book database.

In this part we will create different kind of filter, we will handle permissions management with roles and permissions. We will discuss how to add a file management and some settings to your admin panel.

If you didn’t follow the previous article be sure you got them :

Create filters

What is filters

Filter is an easy way for admin to filter the list of entries of a table. Backpack for Laravel have a lot of filter ready to use and easy to add. Of course you can create your own filter, just take a look at the official documentation or i will take talk about it in an article.

How to add a filter to Laravel Backpack

Adding a filter in laravel backpack is simple. The syntax is pretty similar to add Column and Field. For the 3.6 version while writing this article there’s 8 types of filter. When we adding a filter we need to specify 3 parameters:

  • $options – an array of options (name, type, label are most important)
  • $values – filter values – can be an array or a closure
  • $filter_logic – what should happen if the filter is applied (usually add a clause to the query)

Let’s add a filter to category. A filter to list the categories for a subject just for an example. We adding this piece of code in the setup() method of admin controller located at app/Http/Controller/Admin/CategoryCrudController.php

$this->crud->addFilter([ // select2 filter
            'name' => 'subject_id',
            'type' => 'select2',
            'label'=> 'Subject'
        ], function() {
            return \App\Models\Subject::all()->pluck('title', 'id')->toArray();
        }, function($value) { // if the filter is active
            $this->crud->addClause('where', 'subject_id', $value);
Filter Subject Backpack for laravel
Filter Subject
Filter creation date, Laravel Create admin panel
Filter creation date

As we can see in those screenshot the filters are on the top of the list and help us filter our data. We can add as more as we need.

Handy form

When we have to make data entry most of the time we use form but when the form is long we need a way to make it easier. Laravel Backpack have a very useful feature to achieve that. You can create tabbed form without effort. Let’s see how we can do that.

To used tabbed for we just have to add a tab parameter to the addField() method. For our example of the book database when we have to insert a book the form is long. Let’s split it.

    'name' => 'title',
    'type' => 'text',
    'label' => "Title",
    'tab' => 'Book'
    'name' => 'author_id',
    'type' => 'select2',
    'label' => 'Author',
    'attribute' => 'title',
    'entity' => 'author',
    'model' => 'App\Models\Author',
    'tab' => 'Book'

And yes it’s easy as we showed it but let’s take a look at the result.

Handy form laravel backpack
Handy form laravel backpack

Adding permissions management

When we are building an admin panel one of the most useful feature is permission management. Permission management allow us to say who can do what on the admin panel.

Laravel Backpack have a package to manage permission easily. Let’s install it by executing the corresponded command.

composer require backpack/permissionmanager

This package is developed by a team named spatie you can find the original package on GitHub.

Let’s finish the installation by publishing config file and run the migrations.

php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="migrations"
php artisan migrate
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="config"

Another step is add the Spatie\Permission\Traits\HasRoles trait to your User model(s). Make it look like this :

namespace App;
use Backpack\CRUD\CrudTrait; // <-------- here
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles; // <-------- and here
class User extends Authenticatable
    use Notifiable;
    use CrudTrait; // <-------- Add this one
    use HasRoles; // <-------- also this
     * The attributes that are mass assignable.
     * @var array
    protected $fillable = [
        'name', 'email', 'password',
     * The attributes that should be hidden for arrays.
     * @var array
    protected $hidden = [
        'password', 'remember_token',
     * The attributes that should be cast to native types.
     * @var array
    protected $casts = [
        'email_verified_at' => 'datetime',

One last step add a menu item for it in resources/views/vendor/backpack/base/inc/sidebar_content.blade.php.

  <li class="treeview">
    <a href="#"><i class="fa fa-group"></i> <span>Users, Roles, Permissions</span> <i class="fa fa-angle-left pull-right"></i></a>
    <ul class="treeview-menu">
      <li><a href="{{ backpack_url('user') }}"><i class="fa fa-user"></i> <span>Users</span></a></li>
      <li><a href="{{ backpack_url('role') }}"><i class="fa fa-group"></i> <span>Roles</span></a></li>
      <li><a href="{{ backpack_url('permission') }}"><i class="fa fa-key"></i> <span>Permissions</span></a></li>
User List Backpack for laravel laravelguy
User list
Role list
Role list
Permission List
Permission List

When i am handling permissions on a admin my approach is to create permission first then add my restriction based on permission. If you make your rules based on permissions you can create different roles when you want it without changing the code.

The documentation is pretty clear with his blade directive and methods to handle rules but i will make a special article about handling this rules. For now you can take a look a the documentation to know how to use them.


We are not at the end of the article “Create Admin Panel with Laravel Backpack Part 4” ,in this article we covered how to create filters and install permission manager for backpack. As always if you get any trouble leave a comment and we will fix it together. If you want to be notified when a new article is out. Activate the notification by clicking on the bell at the bottom of your screen.

You may also like...

Leave a Reply

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