1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
//open new session php artisan make:migration:schema create_tags_table --model=0 --schema="name:string:unique" php artisan migrate //Now that we have the tags table in the database, let's generate the actual files we'll be using: # STEP 1. create a model, a request and a controller for the admin panel php artisan backpack:crud tag # STEP 2. add a route for the crud panel (under the admin prefix and auth middleware): php artisan backpack:base:add-custom-route "CRUD::resource('tag', 'TagCrudController');" # STEP 3. add an item to the sidebar menu php artisan backpack:base:add-sidebar-content "<li><a href='{{ backpack_url('tag') }}'><i class='fa fa-tag'></i> <span>Tags</span></a></li>" #The code above will have generated: # #- a migration (database/migrations/yyyy_mm_dd_xyz_create_tags_table.php); #- a database table (tags with just two columns: id and name); #- a model (app/Models/Tag.php); #- a controller (app/Http/Controllers/Admin/TagCrudController.php); #- a request (app/Http/Requests/TagCrudRequest.php); #- a resource route, as a line inside routes/backpack/custom.php; |
What we should notice inside this TagCrudController is that:
As we can tell from the comments there, in most cases we shouldn’t use $this->crud->setFromDb(), which automagically figures out which columns and fields to show. That’s because for real models, in real projects, it would never be able to 100% figure out which field types to use. Real projects are very custom – that’s a fact. In real projects, models are complicated, use a bunch of fields types and you’ll want to customize things. Instead of using setFromDb() then gradually changing what you don’t like, we heavily recommend you manually define all fields and columns you need.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
public function setup() { /* |-------------------------------------------------------------------------- | CrudPanel Basic Information |-------------------------------------------------------------------------- */ $this->crud->setModel('App\Models\Tag'); $this->crud->setRoute(config('backpack.base.route_prefix') . '/tag'); $this->crud->setEntityNameStrings('tag', 'tags'); /* |-------------------------------------------------------------------------- | CrudPanel Configuration |-------------------------------------------------------------------------- */ - // TODO: remove setFromDb() and manually define Fields and Columns - $this->crud->setFromDb(); // for ref to make field :: https://backpackforlaravel.com/docs/3.4/crud-fields // for ref to make column :: https://github.com/Laravel-Backpack/Docs/blob/master/3.4/crud-columns.md + // Columns + $this->crud->addColumn(['name' => 'name', 'type' => 'text', 'label' => 'Name']); + + // Fields + $this->crud->addField(['name' => 'name', 'type' => 'text', 'label' => 'Name']); } } |
The request
Backpack will also generate a standard FormRequest file, that you can use for validation of the Create and Update forms. There is nothing Backpack-specific in here, but let’s take a look at the generated app/Http/Requests/TagCrudRequest.php file:
This file is a 100% pure FormRequest file – all Laravel, nothing particular to Backpack. In generated FormRequest files, no validation rules are imposed by default. But we do want name to be required and unique, so let’s do that, using the standard Laravel validation rules:
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ - // 'name' => 'required|min:5|max:255' + 'name' => 'required|min:5|max:255|unique:tags,name' ]; } |
1 2 3 |
'attributes' => [ 'email' => 'email address', ], |