Slug attribute from a relation

System Information
  • Strapi Version: 3.4.1
  • Database: SQLLite
  • Node Version: v12.16.1
  • NPM Version: 6.14.8

Hi strapi community,

I’ve tried to follow the tutorial “Create a slug system” from the documentation and it works but what I’m trying to achieve is to insert the slug attribute based on attributes from a relation.

Is that possible?

Here is my lifecycle methods inside products model:

const slugify = require("slugify");

module.exports = {
  lifecycles: {
    async beforeCreate(data) {
      if (data.brand.name) {
        console.log(data.brand.name);
        data.slug = slugify(data.brand.name, { lower: true });
      }
    },
    async beforeUpdate(params, data) {
      if (data.brand.name) {
        console.log(data.brand.name);
        data.slug = slugify(data.brand.name, { lower: true });
      }
    },
  },
};

My two collection types that have relation.

Products:

  "kind": "collectionType",

  "collectionName": "products",

  "info": {

    "name": "Products",

    "description": ""

  },

  "options": {

    "increments": true,

    "timestamps": true,

    "draftAndPublish": true

  },

  "attributes": {

    "name": {

      "type": "string"

    },

    "brand": {

      "model": "brands",

      "via": "products"

    },

    "slug": {

      "type": "string"

    }

  }

}

Brand:

  "kind": "collectionType",

  "collectionName": "brands",

  "info": {

    "name": "Brands",

    "description": ""

  },

  "options": {

    "increments": true,

    "timestamps": true,

    "draftAndPublish": true

  },

  "attributes": {

    "name": {

      "type": "string"

    },

    "products": {

      "via": "brand",

      "collection": "products"

    }

  }

}

Did you end up figuring this out? I am trying to implement a slug system similar to the one you mentioned. If so, please share.