In this tutorial, we will be creating a Vue.js 3 client app that communicates with a Laravel API. This course will guide you through the step-by-step process of setting up the client app and connecting it to the Laravel API. By the end of this tutorial, you will have a fully functional client app that can interact with your Laravel API.
Prerequisites:
- Basic knowledge of Vue.js and Laravel.
- Node.js installed on your machine.
- PHP installed on your machine.
- Composer installed on your machine.
Step 1: Setting up Laravel API
-
Create a new Laravel project by running the following command in your terminal:
composer create-project --prefer-dist laravel/laravel api
-
Navigate to the project directory:
cd api
-
Create a new controller by running the following command:
php artisan make:controller ApiController
-
Define your API routes in routes/api.php. Here is an example route that returns a list of users:
Route::get('/users', [ApiController::class, 'index']);
- Define the index method in your ApiController to return the list of users:
public function index() { return User::all(); }
Step 2: Setting up Vue.js 3 Client App
-
Create a new Vue.js project by running the following command in your terminal:
npm create @vitejs/app client
-
Navigate to the project directory:
cd client
-
Install the necessary dependencies by running the following commands:
npm install vue@next npm install vue-router@next npm install axios
- Create a new folder called ‘components’ and create a new component called ‘UserList.vue’. Populate the component with the following code:
<template> <ul> <li v-for="user in users" :key="user.id">{{ user.name }}</li> </ul> </template>
5. Update the 'App.vue' file to include the 'UserList' component:
```html
<template>
<div>
<UserList />
</div>
</template>
<script>
import UserList from './components/UserList.vue';
export default {
components: {
UserList,
},
};
</script>
- Update the ‘main.js’ file to include the VueRouter and Axios configurations:
import { createApp } from 'vue'; import { createRouter, createWebHistory } from 'vue-router'; import axios from 'axios'; import App from './App.vue';
const router = createRouter({
history: createWebHistory(),
routes: [],
});
axios.defaults.baseURL = ‘http://localhost:8000/api‘;
createApp(App).use(router).mount(‘#app’);
Step 3: Connecting the Client App to the Laravel API
1. Run your Laravel API project by running the following command in your terminal:
php artisan serve
2. Run your Vue.js client app by running the following command in a new terminal window:
npm run dev
3. Open your browser and navigate to 'http://localhost:3000' to see your Vue.js client app in action. You should see a list of users fetched from your Laravel API.
Congratulations! You have successfully created a Vue.js 3 client app that communicates with a Laravel API. You can now expand on this project by adding more components, routes, and functionalities to create a fully functional client app for your Laravel API.
Is it possible to purchase the Laraval Vue js course only?
Hello. I'm Korean, so the translation may be awkward, but please understand.
Thanks to you, I always get a lot of help.❤
Until now, I've always exchanged values using axios or inertia to write laravel9 and vue3, sync/async.
However, I don't have a proper concept, so whenever I create an administrator page, I have a problem.🤔
For example, suppose that only some authorized administrators can view information such as statistics on the Administrators page.
I'm going to use domain and multi-tenant to implement authentication conditions, so I was wondering if it would be better to separate the database or leave it in a single state.
It's a topic that's not in the video, but listening to SPA reminded me of my worries, so I wrote it.🥲
If I have a chance, I hope the number of subtitles will increase. I'm also highly recommending these videos to people in our company!
Thank you always for uploading videos related to laravel9+vue3 that are like sweet rain in the drought😉
Hi, would like to ask:
In the About this Course, you mentioned, "This is not a Laravel SPA, it's a front-end course with Vue.js 3".
Could you please elaborate further what do you mean by that?
– As in if it's a Laravel SPA, how different would it be?
Thanks
Thanks
Vue has been driven into the ground with the v2 -> v3 transition. It’s a shame, it was easy to learn.
How to Test for SFTP Connectivity in laravel app
Looks really great quality courses
Are these courses for free?
The laravel api and the vue one?
Could be intersting to be able to buy a one time course, no sub or renewing plan
May I know what's your next video course?
I'am waiting a long time the vue course on your channel. Awesome.
Good one!
Interesting to see Pinia here
Can you make course steps as downloadable?
Awesome