Generate Taxonomy CRUD

Introduction

This is a guide to generating taxonomies crud in the module of VaahCMS. Before jumping to the crud part project should have generated the module and activated it. For the module and theme set up follow the below link: VaahCLI

Create taxonomies

Create a module first. Create Module. One module can have many cruds.
Use the below command to generate users CRUD :

npx vaah cms:taxonomies

After pressing enter.

  This command are only for Vue 3 module      
  For which you want to create CRUD:  (Use arrow keys)
> Module............................................................0
  Theme.............................................................1
  Custom Path.......................................................2
  
  //........................................Press enter on the module
 For which you want to create CRUD:  Module
 Enter the Module/Theme/Entity name:  <module-name>
 Enter the section name (Backend | Frontend or Folder name):  Backend
 Vue folder name/path:  Vue
 Do you want to generate migration file (true/false):  true
 Enter your taxonomy database table name:  hw_taxonomies
 Enter your taxonomy type database table name:  hw_taxonomy_types
 Enter your taxonomy type database table name (singular):  hw_taxonomy_type
 Enter your Taxonomy model name (singular):  Taxonomy
 Enter your Taxonomy type model name (singular):  TaxonomyType
 Enter your controller name (plural):  Taxonomies

Now follow following steps:

step 1)

If you want to add more columns to an existing table then you can add them before migration.

step 2)

Re-activate your module to run migrations. This means deactivate and then activate. it helps to run the migration.

step 3)

Run npm install --save @grapoza/vue-tree command in VaahCms\Modules<module-name> \Vue directory. This command creates a vue tree of taxonomies.

Run npm install --save @grapoza/vue-tree command

step 4)

Include the laravel router file in the module's route file

VaahCms/Modules/<module-name>/Routes/backend.php

Add this code in the last.

//-------------------------------------------------------
include("backend/routes-taxonomies.php");
//-------------------------------------------------------

Step 5)

Include the vue router file.

VaahCms/Modules/<module-name>/Vue/routes/routes.js

Add this code to the above path.

//-------------------------------------------------------
import taxonomies from "./vue-routes-taxonomies";
   
routes = routes.concat(taxonomies);
//-------------------------------------------------------

Step 6)

Call router file when clicking on the Taxonomies

VaahCms/Modules/<module-name>/Vue/components/aside.vue

Add this code to items of the module.
(VaahCms/Modules/<module-name>/Vue/components/aside.vue).

//-------------------------------------------------------
        label: '<module-name>',
        items: [
           {
                label: 'Taxonomies',
                icon: 'fa-regular fa-chart-bar',
                to: "/taxonomies"
            },
        ]
//-------------------------------------------------------

Add code in taxonomy.php in below path
VaahCms/Modules/<module-name>/Models/Taxonomy.php


  //-------------------------------------------------
    use Illuminate\Support\Str;

     public static function getTaxonomyByType($type)
    {
        $tax_type = TaxonomyType::getFirstOrCreate($type);

        $item =array();

        if(!$tax_type){
            return $item;
        }

        $item = self::whereNotNull('is_active')
            ->where('hw_taxonomy_type_id',$tax_type->id)
            ->get();
        return $item;
    }



//----------------------------------------------------
    public static function createItem($request)
    {

        $inputs = $request->all();

        $validation = static::validation($inputs);
        if (!$validation['success']) {
            return $validation;
        }


        // check if name exist
        $item = static::where('name', $inputs['name'])->withTrashed()->first();

        if ($item) {
            $response['success'] = false;
            $response['errors'][] = "This name is already exist.";
            return $response;
        }

        // check if slug exist
        $item = static::where('slug', $inputs['slug'])->withTrashed()->first();

        if ($item) {
            $response['success'] = false;
            $response['errors'][] = "This slug is already exist.";
            return $response;
        }

        $item = new static();
        $item->fill($inputs);
        $item->slug = Str::slug($inputs['slug']);
        $item->save();

        $response = static::getItem($item->id);
        $response['messages'][] = 'Saved successfully.';
        return $response;

    }



    //-------------------------------------------------
    public static function updateItem($request, $id)
    {
        $inputs = $request->all();

        $validation = self::validation($inputs);
        if (!$validation['success']) {
            return $validation;
        }

        // check if name exist
        $item = self::where('id', '!=', $inputs['id'])
            ->withTrashed()
            ->where('name', $inputs['name'])->first();

        if ($item) {
            $response['success'] = false;
            $response['errors'][] = "This name is already exist.";
            return $response;
        }

        // check if slug exist
        $item = self::where('id', '!=', $inputs['id'])
            ->withTrashed()
            ->where('slug', $inputs['slug'])->first();

        if ($item) {
            $response['success'] = false;
            $response['errors'][] = "This slug is already exist.";
            return $response;
        }

        $item = self::where('id', $id)->withTrashed()->first();
        $item->fill($inputs);
        $item->slug = Str::slug($inputs['slug']);
        $item->save();

        $response = self::getItem($item->id);
        $response['messages'][] = 'Saved successfully.';
        return $response;

    }
    //-------------------------------------------------
    public static function validation($inputs)
    {

        $rules = array(
            'name' => 'required|max:150',
            'slug' => 'required|max:150',
            'hw_taxonomy_type_id' => 'required|exists:hw_taxonomy_types,id'
        );

        $messages = array(
            'hw_taxonomy_type_id.required' => 'The type field is required.'
        );

        $validator = \Validator::make($inputs, $rules,$messages);
        if ($validator->fails()) {
            $messages = $validator->errors();
            $response['success'] = false;
            $response['errors'] = $messages->all();
            return $response;
        }

        $response['success'] = true;
        return $response;

    }
  //-------------------------------------------------

Now check this result of taxonomies

For more understanding watch this video.

How to use this taxonomy in other crud.

In this example, we create a shirt taxonomy type and it has four taxonomies. it will show in the shop crud.
taxonomy type is shirt.
taxonomies are sold, refunded, used, and return.

You can check how to create crud in given below link click here

Create shop crud. click here

After Creating the crud of shop.

Add this code in getAssets function in ShopController.php.

$data['shirt_status'] = Hr_taxonomy::getTaxonomyByType('shirt');

Add this code in getAssets function in ShopController.php and Add shirts in shop model fillable.

   <VhField label="Shirt Status">
                    <Dropdown class="w-full"
                              v-model="store.item.shirts"
                              :options="store.assets.shirt_status"
                              optionLabel="name"
                              optionValue="name"
                              id="shirt-status"
                              name="shirt-status"
                              data-testid="shirt-status"
                     />
   </VhField>

The below image shows the taxonomy type and taxonomies list. there is one shirt taxonomy type and four taxonomies.

The below image shows all shirt taxonomies in the dropdown fields.