Exploring the data transfer process through the Vue-Laravel API Resource link

Utilizing the powerful combination of Laravel and Vue connected through an API has been smooth sailing so far. Recently, I made a request to fetch an offer using a method from Vue:

      getOffer(id) {
              this.$http.get('http://127.0.0.1:8000/api/offers/'+id)
                  .then(response => response.json())
                  .then(result => this.offer = result)
          }
      },

The response I received was structured as follows:

{
"body": "xx"
"title": "yy"
}

I stored this data in the 'offer' variable:

    data() {
        return {
            offer: {
                title:'',
                body:''
            }
        }
    },

This data was then utilized in the template:

            <div>
              <h3 class="headline mb-0">{{offer.title}}</h3>
              <div>
                <br>
                {{offer.body}}</div>
            </div>

Everything worked seamlessly up to this point.

However, I decided to switch to using Laravel Resource, which wraps the data within a "data" object in the JSON response. The new structure looks like this:

{
     "data": {
        "body": "xx"
        "title": "yy"
     }
}

My template is now empty - I'm seeking guidance on how to modify the code to function with this new data object format. Moreover, how can I handle scenarios where there are more objects within the "data", such as:

 {
     "data": {
        "body": "xx"
        "title": "yy"
     },
     "data2":{
        "body": "xx"
        "title": "yy"
     },
}

Any insights or suggestions would be greatly appreciated!

Answer №1

Update the getOffer function to utilize result.data instead of just plain result:

  getOffer(id) {
          this.$http.get('http://127.0.0.1:8000/api/offers/'+id)
              .then(response => response.json())
              .then(result => this.offer = result.data)
      }
  },

Functionality has been restored!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are the methods to determine if vue-router is being utilized within the present Vue instance?

I am working on creating a library that includes a vue.js component with navigation elements. The goal is for this component to be flexible, able to function both with and without vue router. When used with the router, it will utilize <router-link> ...

I am unable to pass the req.params.id value as an input to a function located in a separate file

In my project, I've developed a REST API for user and coupon management. The main file driving this API is called coupon-api.js. This file contains the route definitions, while the functions to handle these routes are separated into two distinct files ...

I'm experiencing an issue where the video won't play on my mobile device when I

Why won't my html5 video play on mobile when I tap it? It works fine on desktop, but not on mobile. When I tap the video, it should start playing, but that's not happening. This is the code I'm using: $(document).on('mouseover', ...

Problem encountered when utilizing dat.GUI in a three.js demonstration

I recently experimented with using dat.GUI in a specific three.js example. To integrate a GUI for adjusting mesh opacity, I made the following code modifications. var loader=new THREE.VTKLoader(); loader.load ("models/vtk/bunny.vtk", function(geom){ var ...

A common occurrence is for jQuery/Javascript Ajax POST to generate 6 identical posts when used within a .each loop, happening approximately 20

Context: Building a jQuery mobile phonegap application with multipage-ajaxload and sisyphus enabled form that utilizes an AJAX POST loop to interact with a GUI database. The process involves posting 171 section entries, along with one summary entry to a se ...

An error occurred as the requested resource does not have the necessary 'Access-Control-Allow-Origin' header. The response code received was 403

I am working with Angular products services that make calls to the "http://jsonplaceholder.typicode.com/posts" URL using the HttpClient method. However, I am encountering the error message "No 'Access-Control-Allow-Origin' header is present on t ...

Webpack is having trouble resolving the specified file or directory

MyAPP: |--src   |--index.js   |--content.js |--webpack.config.js index.js : const React = require('react'); const ReactDom = require('react-dom'); const View = require('./content'); ReactDom.render(<View/ ...

Displaying and concealing a subcomponent within a parent element for a set duration of time

I have a notification component that is displayed as a child component in the parent component after a button click. It automatically hides after a certain number of seconds. Here is the code I have developed: MyCode Parent component: <button (click)= ...

Interested in enabling a click event for the .obj file displayed on the webpage?

I am currently working on a web page that displays a .obj file using Three.js. I have successfully implemented the ability to rotate the OBJ model by dragging the mouse left/right thanks to THREE.TrackballControls(). Now, my next goal is to allow users ...

Adjustable <a> component

There is this element: <a id="lnkViewEventDetails" class="psevdo calendar-event event" style="height: 126px;" href="/someurl/895?responseType=5" onclick="event.stopPropagation();"> I am trying to make it resizable using JQuery UI: $("#lnkViewEvent ...

What could be causing jqplot to act in this manner?

When creating a chart with 4 series, I noticed that three of them are displayed as stacked bar charts while the fourth appears as a line. I initially assumed that the order in which I passed the series to the $.jqPlot function didn't matter. I thought ...

Using AngularJS, you can easily preselect an option in a select menu that is populated using ng-options

Exploring how to use AngularJS ng-option directive to populate a select menu and set a default option. The JSON data retrieved by AngularJS: {"AF":"Afghanistan","AX":"\u00c5land Islands","AL":"Albania","DZ":"Algeria","AS":"American Samoa","AD"} The ...

RethinkDB is throwing a ReferenceError because the connection variable has not been defined

Utilizing Express.js and Rethink, I am attempting to execute a straightforward post to a route that saves data to the database. The initial connection is successful and logs the message Connected to Rethink, but when I try to use the connection to insert ...

Adjust the color of the font within a div element when hovering over it

I've been attempting to modify the text color and add an underline when a user hovers over it. Despite trying various methods, I haven't been successful. I scoured the internet for a solution but couldn't find one that met my specific requi ...

The functionality of ui-router appears to be limited when accessed from a nodejs directory, however, it functions properly on

It's strange that ui-router is not functioning as expected on my computer. The setup involves an index.html file serving as a header and test.html as an attached view. Interestingly, it works perfectly fine on Plunker. The content of index.html match ...

The variable "randomString" has not been declared within the HTMLInputElement.onclick function in Types

I need a function that can generate a random string or number for me. Initially, my function in TypeScript looked like this: randomString() { let chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"; let string_length = 8; ...

Loading events for a fullCalendar in node.js using the jade library

I successfully integrated fullCalendar into my nodeJS application using jade and Express, and I have managed to load the calendar. Within the jade file, I pass an array containing events information. How can I display these events on the calendar within th ...

"Effortlessly transform a JSON string into a JSON Object with JavaScript - here's how

When I serialize my object using the ASP.net JavaScriptSerializer class and return it to the client side, how can I deserialize the string in JavaScript? ...

Incorporate photo thumbnails into Material UI cards

I am currently using Material-UI card component in my React web application. The card is quite plain and only has a title. I would like to enhance it by adding a thumbnail on the left side of the card, followed by the title, similar to how song lists are d ...

Customizing React components based on API data

const LinkList = () => { const [links, setLinks] = useState([]); const url = 'http://localhost:5000/xyz'; const hook = () => { console.log('effect'); axios .get(url) .then(respo ...