Courses

How to Build Laravel 12 API From Scratch

Web Client with Vue.js

Summary of this lesson:
- Introduction to consuming Laravel API with Vue.js
- Making API calls using axios
- Using browser developer tools to debug API calls

This lesson will overview a client for our API using Vue.js. This course isn't about Vue.js, but in the feature, we will need to test the API with both clients, like postman, insomnia in a non-visual way and a web view visual way.

This lesson isn't about Vue.js but about how to consume the API.


I have made a Vue.js component called Home and loaded it.

resources/js/app.js:

import { createApp } from 'vue'
import Home from '@/components/Home.vue';
 
createApp({})
.component('Home', Home)
.mount('#app')

The component has a simple HTML code to show categories and products. But the main part of this code I want to show is the script part.

<template>
// ...
</template>
 
<script setup>
import { onMounted, ref } from 'vue';
 
const categories = ref({})
const products = ref({})
 
const getCategories = async () => {
await axios.get('/api/categories')
.then(response => {
categories.value = response.data
})
.catch((error) => console.log(error))
}
 
const getProducts = async () => {
await axios.get('/api/products')
.then(response => {
products.value = response.data.data
})
.catch((error) => console.log(error))
}
 
onMounted(() => {
getCategories()
getProducts()
})
</script>

In this example, we have a typical axios GET call, and then after we have a response, the data is set to a variable.

If you go to your browser developer tools, the primary tabs you want to look at are Console and Network.

In the Console tab, you will see what errors may happen from the API or the Javascript.

We can see all the requests made in the' Network' tab. Besides loading assets, we can see two API requests.

After pressing one of the API calls first in the Headers, we can see the status.

And in the Response, we can see the actual response.

Previous: List inside of List: Multi-Level Data
avatar

I think there is a TYPO in the first paragraph it should be "This course isn't about Vue.js, but in the 'future', we will need to test the API with...." instead of "feature".

I was algo getting an issue "ReferenceError: axios is not defined". I imported and it worked...

import axios from 'axios';
import { onMounted, ref } from 'vue';
👍 2
avatar
You can use Markdown
avatar

I struggled to make Vue work... I needed to change the app.js file from:

createApp({})
    .component('Home', Home)
    .mount('#app')

to:

createApp(Home)
    .mount('#app')
👍 2
avatar

Did you make sure you imported the Home component and createApp for the first approach?

import './bootstrap';
import { createApp } from 'vue'
import Assignments from "./components/Home.vue";

createApp({})
    .component('Home', Home)
    .mount('#app')
avatar
You can use Markdown
avatar

This lesson does not work for me.

avatar
You can use Markdown
avatar

IMHO it will be great if in the example also use laravel as client. for example a microservices that provide product and being called from another laravel microservices that need products and category

avatar
You can use Markdown
avatar

It's not products.value = response.data.data but products.value = response.data After correcting my products awe finally showing.

avatar
You can use Markdown
avatar
You can use Markdown