In this tutorial, using Laravel and Vue.Js, we are going to create a single page application. We will learn who to create, read, update and delete using Vue.Js frontend and Laravel API backend.
Table of Contents
- Install Laravel and NPM Dependencies
- Create Migration, Model and Controller
- Define Laravel Routes
- Create Vue App
- Create Vue Components
- Define Vue Routes
- Import All to app.js
- The Output
Step 1 : Install Laravel and NPM Dependencies
Each Laravel project needs this thing. That’s why I have written an article on this topic. Please see this part from here: Install Laravel and Basic Configurations.
After installing Laravel, run this command to install frontend dependencies:
We need to install vue-router and vue-axios. vue-axios will be used for calling Laravel API. Let’s install these:
After installing all dependencies run this command:
This
npm run watch
command will listen for file changes and will compile assets instantly. You can also run this command npm run dev
. It won’t listen for file changes.Step 2 : Create Migration, Model and Controller
We are going to create Book model, migration and controller. We will add, read, update, delete book. Run this artisan command to create these 3 at once:
php artisan make:model Book -mcr
Now open create_books_table.php migration file from database>migrations and replace up() function with this:
create_books_table.php
Migrate the database using the following command:
php artisan migrate
Open Book.php model from app folder and paste this code:
Book.php
We need to define the index, add, edit, delete methods in BookController file. Open BookController.php from app>Http>Controllers folder and paste this code:
BookController.php
Step 3 : Define Laravel Routes
Our model, migration and controller are ready to use. Let’s define the web and API routes. Open web.php from routes folder and register this route:
web.php
Open api.php and define CRUD routes like these:
api.php
Step 4 : Create Vue app
To declaratively render data to the DOM using Vue.js we need to declare Vue app. Navigate to resources>views folder and create a file called app.blade.php. Then paste this code:
app.blade.php
Step 5 : Create Vue Components
We are going to add 4 Vue components.
- App.vue
- AllBooks.vue
- AddBook.vue
- EditBook.vue
App.vue is the main Vue file. We will define router- view in the file. So all route pages will be shown in the App.vue file
Go to resources>js folder and create a file called App.vue and paste this code:
App.vue
In the resources>js create a folder called components and go to the folder. We are going to create the rest 3 Vue components here. We So, let’s create.
AllBooks.vue
AddBook.vue
EditBook.vue
In the three files, we have used Axios to call Laravel API.
Step 6 : Define Vue Routes
In the resources>js folder, create a file named routes.js and paste this code:
routes.js
Step 7 : Import All to app.js
We are about to finish. This is the last step. Open/create app.js from resources>js folder and paste this code:
app.js
https://www.mynotepaper.com/laravel-vue-crud-single-page-application-tutorial