Setting up CRUD in the Backend

We'll begin the tutorial with the VaahCMS backend where the CRUD operations are fulfilled. I'll keep this part brief as CRUD is a topic covered extensively elsewhere and I assume you are already familiar with the basics of Laravel.

In summary, we will:

  • Set up a database and model
  • Set up backend routes
  • Define methods in the controller to perform the crud Operations
# Set up a database and model

In Step 4, we already generate migration and set up a database.

Now we are going to create a model for a table. Models typically live in the app\Models directory, but you are free to place them anywhere that can be auto-loaded according to your composer.json file. All Eloquent models extend Illuminate\Database\Eloquent\Model class.

The easiest way to create a model instance is using the make:model Artisan command:

vaah cms:m-make model Articles Content

This will generate a model file at VaahCms/Modules/Articles/Entities/. Let's edit the model file to following:

# Set up Backend routes

Now we set up backend routes that our Vue application will need. All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by the framework. In Vaahcms, the Routes/backend.php file defines module routes that are for your web interface.

The router allows you to register routes that respond to any HTTP verb:

Let check the VaahCms/Modules/Articles/Routes/backend.php

# Set up and define method in Controller

Controller acts as a directing traffic between Views and Models. In this application, we also define crud methods which is directing traffic to Models.

The easiest way to create a controller instance is using the make:controller Artisan command:

vaah cms:m-make controller Articles Content

This will generate a Controller file at VaahCms/Modules/Articles/Http/Controllers/Backend/. Let's edit the Controller file to following: