Recently had a project where I needed to get a CRUD admin up and running quick. My go to framework for CRUD is Django Admin. However Python/Django isn’t always an option for various technical or political reasons. In this case it had to be PHP. So I looked high and low for an out of the box framework that would allow editing of tables and thankfully Backpack Crud (docs, github) was there.
What is CRUD?
The dirty secret:
We’re all just building CRUD apps.
— I Am Devloper (@iamdevloper)
I didn’t really want to roll my own admin interface (been there done that), so I went ahead and installed Backpack Crud.
After you get Composer installed, Laravel installed and Backpack Crud installed, you should be ready to start adding models / controllers.
Backpack Crud has pretty solid documentation included a nice getting started page. I’m doing this write up to to document the extra things I ran into (see example below).
First off, as of January 2018, I ran into dependency conflicts with Laravel 5.5 and Backpack Crud 3.3. I ended up going with this in my composer.json file:
How to setup a Backpack Crud model:
1) Run the generator:
Say you need CRUD for table of colors with fields: name, sort order and hex code.
First run:
That will auto generate the model, request and controller files. Then it is up to you to customize the contents of each file. You will also need to setup migrations for the new models.
2) Setup Model (app/models/Color.php):
The model is a straightforward Eloquent model with use CrudTrait;.
What tripped me up at first was you have to set the $fillable property on the model or else nothing shows up! So at a minimum you need to list all the columns you want the user to edit in $fillable.
3) Setup Request (app/Http/Requests/ColorRequest.php)
4) Setup Controller (app/Http/Controllers/Admin/ColorCrudController.php)
If you are setting up lots of controllers and copy / pasting watch out for the path to StoreRequest and UpdateRequest, it needs to match. IntelliJ hides the namespace and use section by default so I didn’t see that. I wasted some time tracking down some really strange errors with form validation labels.
The setFromDb() command is what wires up everything, but I found I had to comment that out and manually add all the fields (add/edit page) and columns (listing page) since the schema I was handed had the columns in a different order than made sense on the admin screens. For a really simple table setFromDb() is probably fine though.
5) Setup routes (web.php)
6) Add link to side bar (resources/views/vendor/backpack/base/inc/sidebar.blade.php)
What is great about Backpack Crud:
Where I’d like to see Backpack Crud improve:
This content was originally published here.