Simply copy and paste the following command line in your terminal to create your first Strapi project.
npx create-strapi-app
my-project
This tutorial is part of the « E-commerce website with Strapi, Nuxt.js, GraphQL and Strip »
Note: The source code is available on Github.
Congratulations, you successfully displayed the list of restaurants! This was the first major step. The next step is to create dishes that go with the restaurants.
Every restaurant sells dishes, which also must be stored in the database. So, you now need to create a new Content Type named dish
.
Dish
for the Display name, and click Continue.name
in the Name field.image
under the Name field, and check the Single media option then click Finish.price
under the Name field, then click Finish.Choose the Relation field, this one is a bit more specific. Your purpose here is to tell to Strapi that every dish can be related to a restaurant. To do so, create a one-to-many relation, restaurant has many Dishes as below, then click Finish.
Finally, click Save and wait for Strapi to restart.
Well done! You created your second Content Type. The next step is to add some dishes to your database.
Add some entries.
Create as many dishes as you would like to see in your app. If you're lacking some inspiration, you can check Deliveroo 🙈 . In the next step, you will be able to request them from the Strapi API.
We have just added some new dishes. We need to make sure that the content is publicly accessible through the API:
It looks you are going to the right direction. In the next step, you will display these dishes in your Nuxt.js app.
At the end of this section, you should have a Dishes page that looks like this:
Inside the pages
directory, create a new directory restaurant
. Nuxt reads all the .vue
files inside the pages
directory and automatically creates the router configuration for you, checkout the Pages directory to learn more about this.
To create restaurants pages we need to add a new .vue
file with the restaurant name, but because we don't know the name of the page due to it coming from the Strapi API, we will need to create a dynamic page, to do so you need to add an underscore before the .vue
file name or before the name of the directory, if you want the directory to be dynamic.
Dynamic pages can be created when you or you don't want to have to create the same page over and over again.
Let’s create a new _id.vue
file inside the restaurant
directory and prefix it with an underscore, like this pages/restaurants/_id.vue
, and copy/paste the following code in it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// pages/restaurants/_id.vue
<template>
<div class="uk-container uk-container-xsmall">
<span class="uk-heading-small">
// Link to go back to the previous page
<NuxtLink class="uk-button uk-button-text" to="/">
<span uk-icon="arrow-left"></span> go back
</NuxtLink>
</span>
// Displaying dishes
<div v-for="dish in dishes" :key="dish.id">
<div class="uk-card uk-card-default uk-child-width-1-2 uk-margin" uk-grid>
<div class="uk-card-body uk-card-small">
<h2 class="uk-card-title">{{ dish.attributes.name }}</h2>
<p>{{ restaurant.data.attributes.name }}</p>
<p>{{ dish.attributes.price }} €</p>
<button class="uk-button uk-button-primary uk-margin-xlarge-top">
Add to cart
</button>
</div>
<figure class="uk-card-media-right uk-cover-container">
<img
:src="getStrapiMedia(dish.attributes.image.data.attributes.url)"
:alt="dish.attributes.image.data.attributes.alternativeText"
/>
</figure>
</div>
</div>
</div>
</template>
<script>
import { getStrapiMedia } from '@/utils/media'
import restaurantQuery from '@/apollo/queries/restaurant'
export default {
data() {
return {
restaurant: Object,
}
},
apollo: {
restaurant: {
prefetch: true,
query: restaurantQuery,
variables() {
return { id: this.$route.params.id }
},
},
},
computed: {
dishes() {
if (!this.restaurant?.data) return []
return this.restaurant.data.attributes.dishes.data
},
},
methods: {
getStrapiMedia,
},
}
</script>
The restaurant page display a “go back” link to go back to the previous page, using apollo wi request and display the current restaurant dishes.
Again, let’s create the graphql
template. Add a new file apollo/queries/restaurant.gql
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
query Restaurant($id: ID!) {
restaurant(id: $id) {
data {
id
attributes {
name
dishes {
data {
id
attributes {
name
price
image {
data {
attributes {
url
}
}
}
}
}
}
}
}
}
}
Our restaurant.gql
query, ask for specific fields on the restaurant object, and the dishes
field returns an array of dishes.
The dishes page should be accessible from [http://localhost:3000/restaurants/1](http://localhost:3000/restaurants/1)
where 1
is the id of the restaurant.
Nothing particular here: exactly like for the restaurants, you defined a template and then added the logic in the script section.
🛒 In the next section, you will learn how to create a full featured shopping card.
Pierre created Strapi with Aurélien and Jim back in 2015. He's a strong believer in open-source, remote and people-first organizations. You can also find him regularly windsurfing or mountain-biking!