From the course: Building Laravel and Vue.js 2 Web Apps

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Calling an API from Vue

Calling an API from Vue

From the course: Building Laravel and Vue.js 2 Web Apps

Start my 1-month free trial

Calling an API from Vue

- [Instructor] Before we start making calls to the API from our Vue component, we'll need to build out this upsert action in the Category controller a bit. First, let's inject the incoming request which we can do by just creating a parameter of type, Request. We're going to need this to update all existing categories and store any new categories. We'll just be posting our categories data property from the component, so we can store that for categories, and we can access that now with request, post, categories, like that. Then we'll loop through them, for each categories, and again, I'm going to use cat as a looping variable, and we can say, if the ID isn't empty, or isn't zero, then we can use the Category::where method to find the category that has that ID, and then we can call update, passing that cat array of keys and values that we want to update for that model. Otherwise, we can just call Category::create, passing the same array of keys and values. Since Vue needs to be made…

Contents