After setting up your first Strapi app with PostgreSQL, it’s time to dive deeper into creating and managing your content. In this blog post, we will walk through the process of creating Content Types (Articles and Categories), setting up relationships between them, and utilizing these relationships to organize your content effectively.
Step 1: Create the Category Content Type
To start, we’ll create a Category content type to help us categorise our articles. If you haven’t followed the blog on setting up the Strapi app, check out our previous blog to setting up your first strapi app with PostgreSQL.
Create Category Content Type by following these steps:
-
Access the Content-Types Builder:
-
Open your Strapi admin panel (
http://localhost:1337/admin
). -
Click on “Content-Types Builder” in the left sidebar.
-
-
Create a New Collection Type:
-
Click the “Create new collection type” button.
-
Enter
Category
as the name for your content type and click “Continue”.
-
-
Add Fields to the Category Content Type:
-
Click “Add another field” and select the following fields:
-
name
: Text (Short text) -
description
: Text (Long text)
-
-
Click “Save” in the top right corner.
-
Strapi will automatically generate the necessary API endpoints for the Category content type.
Step 2: Create the Article Content Type
Next, we’ll create the Article content type, which will include a relationship with the Category content type.
Follow these steps:
-
Create a New Collection Type:
-
Go back to the “Content-Types Builder”.
-
Click the “Create new collection type” button.
-
Enter
Article
as the name for your content type and click “Continue”.
-
-
Add Fields to the Article Content Type:
-
Click “Add another field” and select the following fields:
-
Title
: Text (Short text) -
Description
: Rich Text -
Slug
Text (Short text)
-
-
Click “Save” to add these fields.
-
-
Add a Relationship Field:
-
While still editing the Article content type, click “Add another field”.
-
Select “Relation” from the field types.
-
Choose the relation type as
Article
–Category
with a many-to-many relationship (i.e., articles has and belongs to many articles) -
Click “Finish” and then “Save” in the top right corner.
-
Strapi will update the API endpoints to include this relationship.
Step 3: Add Categories
With the content types in place, let’s add some categories:
-
Go to Content Manager:
-
Click on “Content Manager” in the left sidebar.
-
-
Add New Categories:
-
Select
Categories
from the available content types. -
Click “Add New Category”.
-
Fill in the
Name
andDescription
fields. -
Click “Save” to add the category.
-
Repeat this process to add multiple categories as needed (e.g., Technology, Health, Travel).
-
Step 4: Add Articles
Now, we’ll add some articles and assign them to categories:
-
Go to Content Manager:
-
Select
Articles
from the available content types.
-
-
Add New Articles:
-
Click “Add New Article”.
-
Fill in the
Title
,Description
, andSlug
fields. -
Assign the article to a category by selecting a category from the dropdown list in the Category field.
-
Click “Save” to add the article.
-
Repeat this process to add multiple articles and assign them to different categories.
-
Step 5: Accessing Articles and Categories via the API
Settings > Roles below USERS & PERMISSIONS PLUGIN > Public > [your content-types]
With your content in place, you can now access it via Strapi’s RESTful API. Here are a few example endpoints:
Get all categories:
GET http://localhost:1337/api/categories
Get all articles:
GET http://localhost:1337/api/articles
Get articles in a specific category: You can filter articles by category using query parameters. For example:
GET http://localhost:1337/api/categories?fields[0]=name&populate[articles][fields][0]=Title
Conclusion
You’ve successfully created Content Types and set up relations in Strapi. This powerful relationship allows you to organize and manage your content more effectively. With Strapi’s flexible and user-friendly interface, managing complex content structures becomes a breeze.
Explore Strapi’s extensive documentation to learn more about advanced features and customization options. Stay tuned for more tutorials on how to leverage Strapi for your projects. Happy coding!
This article is written by Ken Le, a Sitefinity and .Net Core developer from Websparks.
Strapi is an open-source headless CMS.
Find out more about headless CMS.