Book Image

Full-Stack Vue.js 2 and Laravel 5

By : Anthony Gore
Book Image

Full-Stack Vue.js 2 and Laravel 5

By: Anthony Gore

Overview of this book

Vue is a JavaScript framework that can be used for anything from simple data display to sophisticated front-end applications and Laravel is a PHP framework used for developing fast and secure web-sites. This book gives you practical knowledge of building modern full-stack web apps from scratch using Vue with a Laravel back end. In this book, you will build a room-booking website named "Vuebnb". This project will show you the core features of Vue, Laravel and other state-of-the-art web development tools and techniques. The book begins with a thorough introduction to Vue.js and its core concepts like data binding, directives and computed properties, with each concept being explained first, then put into practice in the case-study project. You will then use Laravel to set up a web service and integrate the front end into a full-stack app. You will be shown a best-practice development workflow using tools like Webpack and Laravel Mix. With the basics covered, you will learn how sophisticated UI features can be added using ES+ syntax and a component-based architecture. You will use Vue Router to make the app multi-page and Vuex to manage application state. Finally, you will learn how to use Laravel Passport for authenticated AJAX requests between Vue and the API, completing the full-stack architecture. Vuebnb will then be prepared for production and deployed to a free Heroku cloud server.
Table of Contents (11 chapters)

Persisting saved listings


The mechanism for persisting saved listings is as follows: when a listing is toggled in the frontend app, we trigger an AJAX request that POSTs the ID to a route on the backend. This route calls a controller that will update the model:

Figure 9.9. Persisting saved listings

Let's now implement this mechanism.

Creating an API route

We'll begin on the server side and add a route for the frontend to POST listing IDS to. We'll need to add the auth middleware so that only authenticated users can access this route (we'll discuss the meaning of :api shortly).

routes/api.php:

...

Route::post('/user/toggle_saved', 'UserController@toggle_saved')
  ->middleware('auth:api')
;

Since this is an API route, its full path will be /api/user/toggle_saved. We haven't yet created the controller that this route calls, UserController, so let's do that now:

$ php artisan make:controller UserController

In this new controller, we'll add the toggled_saved handling method. Since this is an HTTP...