Backend Back to HelloVib

CRUD in 3 Steps

STEP 1. THE MODEL

You can create powerful tables and CRUD fast and easy from any Laravel model class. To accomplish this, first thing to do is to include the Vibrant Tablable trait in your model. For example:

Now, there are a few mandatory protected properties and constants you also have to include in your model. These are:

  • $searchable_fields - (array) Fields to include in queries when a search is performed.
  • $available_fields - (array) Fields that will be available to show in the html table, these can be from the model's original table or appended attributes.
  • $hidden_fields - (array) Fields included in the available fields array that are hidden by default. Users can toggle its visibility.
  • $unsortable_fields - (array) Fields included in the available fields array that table cannot be sorted by.

Your model now will look something similar to this:

Let's declare our CRUD form right here. Yes! We can do this thanks to the vibForm component that accepts an array of fields and its parameters. Notice how we are even telling the VIEW how to present this form. You can do this right at the Model, at the Controller or at the View, it's really up to you.

That's all we need in our model for the basic set up. Not a big deal right?. More advanced implementations include declaring your own custom methods for searching, filtering and sorting. Please check the example files included in the package for more details on options and customizations.

STEP 2. THE CONTROLLER

To make things really easy and quick we will just extend the provided CrudController and define a couple of things at the children's constructor.

We just defined the model we want to CRUD and the path of the blade views that will handle the UI.

That's all we need for a basic controller. Again, if you're looking for more tips please check the provided controller in the examples folder.

STEP 3. THE VIEW

The final step in our road to a beauty and powerful CRUD is the View. Fortunately, the vibTable component and the work done in the previous steps make our live easier now and we only have to worry about the table's look and behavior and not about the data it shows. Play with the options in the component manager to get the settings exactly as you need it. Below you will see an example of the couple of lines you need to include in your views.

Notice that edit and create forms are called the same way at the view, just the passed $form_settings array is a little bit different. These arrays ($table_settings and $form_settings) are create and passed to our views automatically.

TIP Remember to have a look to the example files to check the view conventions we used to make everything flow seamlessly.

EXTRA TIP: ROUTES SHORTCUT

You don't need to worry about creating repetitive routes either. Use the provided 'crud' shortcut and get everything routed in a single line of code. For example:

We are all set. Check the result here.