What is laravel backpack and why we use it?
The backpack is a collection of laravel packages that help us to create a quick and customized admin panel. Its main goal is to provide a code for admin panels and eliminate writing code that repeats in every module.
Installation:
>> run the below command in your root project directory
composer require backpack/crud
>> now run the second command
php artisan backpack:base:install
>> now run the below command for crud functionality
php artisan backpack:crud:install >> after the run the below command for starting the laravel application php artisan serve >> open the below link in your browser http://localhost:8000/admin You can see a login panel. For first time you need to click on register, because you have not any credential yet. So fillup the registration from and login. Now the dashboard is empty and you need to set up the admin or customized it. Crud Operation: Let give an example, start with category CRUD CategoryCrudController:
<?php namespace App\Http\Controllers\Admin;
use Backpack\CRUD\app\Http\Controllers\CrudController; use App\Http\Requests\CategoryCrudRequest; class CategoryCrudController extends CrudController { use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation; use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation; use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation; use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation; use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation; public function setup() { $this->crud->setModel(“App\Models\Category”); $this->crud->setRoute(“admin/category”); $this->crud->setEntityNameStrings(‘category’, ‘category’); } public function setupListOperation() { $this->crud->setColumns([‘name’, ‘slug’]); } public function setupCreateOperation() { $this->crud->setValidation(CategoryCrudRequest::class); $this->crud->addField([ ‘name’ => ‘name’, ‘type’ => ‘text’, ‘label’ => “Tag name” ]); $this->crud->addField([ ‘name’ => ‘slug’, ‘type’ => ‘text’, ‘label’ => “URL Segment (slug)” ]); } public function setupUpdateOperation() { $this->setupCreateOperation(); } } |
Create - using a create form List - using AJAX DataTables Update - using an update form Delete - using a button in the list view Show - using a button in the list view These are the basic operations, that we can do with the help of backpack, at the same time we can use the Eloquent model for the above operations. setupCreateOperation() and setupUpdateOperation(): you are able to define what fields can show in admin ui when creating or updating entries. Using backpack, we can use all other functionalities, that needed for admin panel, like listing the entries, filter the datas, even we can create button using backpack.
Senior Software Engineer at Openweb Solutions