Backpack for Laravel

Backpack for Laravel

A collection of Laravel packages to build a custom admin panel.

Guides    Discussions

How to use (full example)

New Documentation Available

Please use the documentation on our main website. It's bigger, better, faster, prettier. And it's the one we'll keep up-to-date.

This page will help you understand how Backpack\CRUD works, how you can use it and ends with a tip of how you can do it all very very fast.

The easiest way to read this:

  1. Read everything except the blue bubbles. They go in a bit of detail.
  2. Read the blue bubbles. If you don't understand what they're talking about, re-read that section.
  3. You got this. Start coding!


Backpack\CRUD is a fast way to build admin panels with Create, Read, Update, Delete functionality. One panel provides functionality for CRUD operations on one Eloquent Model.

Most of the times, a CRUD Panel consists of:

  • a database table (and maybe connection tables for relationships);
  • a Model;
  • a Controller for the CRUD operations;
  • 0, 1 or 2 Request files (if form validation is needed);
  • a route;

Simple example

Let’s take the Tag entity and create a CRUD Panel for it. We're going to create the CRUD Panel manually, which will take some time but is useful in order to understand how it works. At the end of this page you'll be presented with a way to do it all in under a minute, using generators.

Its CRUD interface consists of:

  • a database table (tags)
  • a model (app/Models/Tag.php)
  • a controller (app/Http/Controllers/Admin/TagCrudController.php)
  • a request (app/Http/Requests/TagCrudRequest.php)
  • a route

The database table

Of course, any entity stored in a database needs at least a table. The tag entity needs one too, “tags”, which looks like this:

Nothing fancy here.

The model

On your standard Eloquent model, make sure you:

  • use the CrudTrait;
  • properly define your relationships to other Models (to be able to use them in the CRUD interface);
  • define either $guarded or $fillable for the CRUD to know which columns are editable;
  • [optional] store it in app/Models (because you may end up having a lot of them);

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;

class Tag extends Model {

	use CrudTrait;


	protected $table = 'tags';
	// protected $primaryKey = 'id';
	// protected $guarded = [];
	// protected $hidden = ['id'];
	protected $fillable = ['name'];
	public $timestamps = true;



	public function articles()
        return $this->hasMany('App\Models\Article', 'article_tag');




The controller

As an official convention:

  • the controller is stored in app/Http/Controllers/Admin
  • its filename is EntityCrudController.php

But you can can use another location/name if you want to. Take a look at it:

<?php namespace App\Http\Controllers\Admin;

use Backpack\CRUD\app\Http\Controllers\CrudController;

// VALIDATION: change the requests to match your own file names if you need form validation
use App\Http\Requests\TagCrudRequest as StoreRequest;
use App\Http\Requests\TagCrudRequest as UpdateRequest;

class TagCrudController extends CrudController {

	public function setup() {
        $this->crud->setRoute(config('backpack.base.route_prefix')  . '/tag');
        $this->crud->setEntityNameStrings('tag', 'tags');

	'name' => 'name',
	'label' => 'Tag name'

	public function store(StoreRequest $request)
		return parent::storeCrud();

	public function update(UpdateRequest $request)
		return parent::updateCrud();

Because your controller (TagCrudController) extends the default CrudController class, every CRUD functionality is available for you to use. If you don't like a functionality, you can:

  1. take a look at how it's done in CrudController;
  2. place a method with the same name in your TagCrudController, with the custom logic you need.


For most cases, your entire panel's logic will be in the setup() method; this is called in the constructor, so anything you customize there will be applied to all CRUD operations; you have access to all variables you need here (the request, the crud, etc); this is where you'll define the basics for the CRUD and specify columns, fields.

The store() and update() functions are needed in each EntityCrudController because the Request needs to be type-hinted in Laravel in order for validation to happen; it's also where you can do custom operations before or after the item is inserted/updated - no callbacks needed.

The requests

This is where you write your validation rules for the Create/Update operations. The app/Http/Requests/TagCrudRequest.php file:

<?php namespace App\Http\Requests;

use App\Http\Requests\Request;

class TagCrudRequest extends \Backpack\CRUD\app\Http\Requests\CrudRequest {

     * Determine if the user is authorized to make this request.
     * @return bool
    public function authorize()
        // only allow updates if the user is logged in
        return \Auth::check();

     * Get the validation rules that apply to the request.
     * @return array
    public function rules()
        return [
            'name' => 'required|min:5|max:255'


Validation not necessary?

If you need form validation on the create/update views, this request file is necessary. Otherwise, just point the controller to the Backpack\CRUD\app\Http\Requests\CrudRequest class instead, which has no validation rules on it, but does require the user to be logged in.

Differences between the Create and Update validations?

If your Update operation requires a different validation than the Create operation, you can create separate request files and use them in you TagCrudController, for ex: UpdateTagRequest.php and CreateTagRequest.php.

The route

The CRUD routes are based on RESTful controllers, so you only need to define one line for each CRUD Panel.


// Admin Interface Routes
Route::group(['prefix' => 'admin', 'middleware' => 'admin'], function()
  // Backpack\CRUD: Define the resources for the entities you want to CRUD.
    CRUD::resource('tag', 'Admin\TagCrudController');
  // [...] other routes

It's generally a good idea to have the admin routes in a separate file. That's why Backpack 3.4+ publishes the routes/backpack/custom.php file, where you can put new admin routes, behind the middleware and guard defined in the config file.

It should look something like this:


// --------------------------
// Custom Backpack Routes
// --------------------------
// This route file is loaded automatically by Backpack\Base.
// Routes you generate using Backpack\Generators will be placed here.

    '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 resources and other admin routes
    CRUD::resource('monster', 'MonsterCrudController');
    CRUD::resource('icon', 'IconCrudController');
    CRUD::resource('product', 'ProductCrudController');
}); // this should be the absolute last line of this file

Note: if you want the item to appear in the top admin menu, it’s as easy as including it in the views/vendor/backpack/base/inc/sidebar_content.php file:

<li><a href="{{ backpack_url('tag') }}"><i class="fa fa-tag"></i> <span>Manage Tags</span></a></li>

The result

The table (or list) view:

The insert (or add) view:

The edit (or update) view:

Congratulations, you now understand how Backpack\CRUD works! This is a very very basic example, but the process will be identical for tables with 50+ columns, complicated logic, etc.

Scroll through the stock functionality

Doing it all very fast

Here's what you can use, to create each file for the CRUD interface very fast:

So when you use generators, the entire process is:

# STEP 1. create migration
php artisan make:migration:schema create_tags_table --model=0 --schema="name:string:unique"
php artisan migrate

# STEP 2. create a model, a request and a controller for the admin panel
php artisan backpack:crud tag #use singular, not plural

# STEP 3. add a route to routes/backpack/custom.php (under the admin prefix and auth middleware): 
php artisan backpack:base:add-custom-route "CRUD::resource('tag', 'TagCrudController');"

# STEP 4. add a sidebar item
php artisan backpack:base:add-sidebar-content "<li><a href='{{ backpack_url('tag') }}'><i class='fa fa-tag'></i> <span>Tags</span></a></li>"

That's a lot faster, right? Don't worry, you'll get used to it, and it'll get faster. Plus, a user interface to generate all this is in the works, if that's more your type of thing.

Please note: You still have to go through the created files, check everything's alright and customize them to your specifications, but a lot of the heavy-lifting is already done. Most notably, you'll need to go in the generated Model and define the $table, $primaryKey and $fillable.

Also, it is NOT recommended that you keep $this->setFromDb(), which automatically tries to figure out what columns and fields you need. It's only smart enough for super-simple CRUDs. Best to insert your own columns and fields, using the syntax that's already there, but commented out. Check out the API cheatsheet to see what's available, and browse through our 44+ field types, 20+ column types and 10+ filter types.

How to use (full example)

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.