Wednesday, 21st November 2018
21 November 2018
laravel vue spa

Create a Laravel Vue Single Page App in Under an Hour

laravel vue spa

Laravel has become the most popular choice for developing PHP projects. One important reason for this popularity is the built in support for Vue.js, a very fast growing JavaScript library for developing impressive front-ends.

This combination results is fast, secure and very impressive applications that need minimum time to go from ideation to final code review. The support for Vue.js means that Laravel developers could use Vue components easily within their apps without wasting time in writing integrations for the components. To demonstrate the support, I decided to create a single page app in Laravel with a Vue.js powered frontend.

Prerequisites

For the purpose of this tutorial, I assume that you have a Laravel application installed on a web server. My setup is:

  • WAMP/XAMPP environment
  • Laravel 5.5
  • PHP 7.1
  • MySQL
  • Node.js with NPM

To make sure that that I don’t get distracted by server level issues, I decided to host my Laravel on Cloudways managed servers because it takes care of server level issues and has a great devstack right out of the box. You can try out Cloudways for free by signing for an account and then following this simple GIF for setting up the server and the Laravel application.

 

Install Node.js with NPM

The first step is the installation of Node.js with NPM.

For this first install Node.js. Next go to the project’s folder and type the following command in the terminal:

This command will install all the JavaScript dependencies for VueJS. In addition, the command will also install laravel-mix, an API for defining webpack.

Configure the Database

Now setup the MySQL database and configure it in Laravel.

In the project root, you will find the .env and config/database.php files. Add the database credentials (username, DB name, and password) to setup the database and allow the Laravel Single page app to access it.

 

Create the Migrations

In the third step, open the terminal and go to the root of the newly created Laravel project and generate a new migration to create task table:

Next , open the migration file located in the database/migration folder and replace the up() function with the following code:

Next , In the app/Providers/AppServiceProvider.php file, the boot method sets a default string length:

Run the Migration

Create the tables in the database by using the following command:

Setup User Authentication

Laravel provide default user authentication in which you can register users who can then login through the provided login system. This login system also provides Laravel CRSF authentication token to further strengthen the security of the application against malicious exploits. Use the following command to set up user authentication in the Laravel Vue SPA:

laravel banner cta

Create Task Model and Task Controller

Create task model because I will handle database operations through Laravel Eloquent. I also need a controller to handle user requests such as create, read, update and delete operations.

Use the following command to create the model and the controller:

Next open the Task Model which in app/Task.php and controller at /app/Http/Controllers/TaskController.php. Update the model code with the following code.

The Code for Controller

Next, update the controller file with the following code.

Middleware

To setup middleware, add the following code to the Task Controller.

Create the Method

In the store() method of Task Controller, update the following code to add data into database.

Update Method

In Update() method of Task Controller, update the following code to edit database data. database.

Delete Method

In the Destroy() method of Task Controller,  the following code will delete data from the database.

Route Set up in Laravel SPA

Routes are located in route/web.php and contains the following code:

Create Vue.js Components

Create a new file for Task component inside /resources/assets/js/components/ folder named Task.vue and add following sample code:

The component is ready for registration. Open app.js file from /resources/assets/js/app.js and add the following line after example component registration line:

Compile Assets

Use the following command to compile the newly added code as a Vue.js component. This will also register component:

Calling in the View

Now open home.blade.php located in /resources/views/ and update it as follows:

Create Update & Delete in Task.vue

The Task component is located inside /resources/assets/js/components/ and is named Task.vue. Open this file and update the code:

Now run the following command to compile the newly added code as a Vue.js component:

Close