I need to display the product name associated with the product_id found in the line_items array within the order table. I aim to achieve this functionality by utilizing Laravel with Vue.js

In my database, there is a cell called line_items in the orders table that contains data like:

  [
      {"customer_id":"30","product_id":"10","unit_id":"2","quantity":"1","price":"2700","total_price":"2700"},
      {"customer_id":"30","product_id":"43","unit_id":"1","quantity":"5","price":"7","total_price":"35"}
    ]

Additionally, I have a products table where the product names are stored in a cell called name. The product_id in the data above corresponds to the IDs in the products table.

Now, there is a route set up like:

 Route::get('json/{order}', 'ReturnController@json')->name('json');

Below is the json function in the ReturnController:

 public function json(Order $order)
    {
        return response()->json([ 'orders' => $order ]);
    }

When accessing the route, the json output is as follows:

{"order":{"id":7,"company_id":1,"order_type":1,"order_no":"12","date":"2019-01-16","status":"1","transaction_raw":[{"amount":"82264","transaction_type":3,"payment_type":1,"owner_type":"App\\Model\\Customer","owner_id":"1"},{"amount":"0","transaction_type":4,"payment_type":1,"owner_type":"App\\Model\\Customer","owner_id":"1","account_head_id":1}],"line_items":[{"customer_id":"1","product_id":"10","unit_id":"2","quantity":"5","price":"2700","total_price":"13500"},{"customer_id":"1","product_id":"43","unit_id":"1","quantity":"52","price":"7","total_price":"364"},{"customer_id":"1","product_id":"9","unit_id":"2","quantity":"18","price":"3800","total_price":"68400"}],"total":82264,"discount":0,"sub_total":82264,"paid":0,"due":82264,"supplier_id":0,"customer_id":1,"others_fin":"{\"transport\":\"0\",\"type\":\"income\"}","created_at":"2019-01-16 19:13:27","updated_at":"2019-01-16 19:13:27"}}

For the vue code, we have:

 <tr v-for="order in orders.line_items">
     <td><input name="" v-model="order.product_id"></td>
     <td><input name="" v-model="PRODUCT NAME"></td>
     <td><input name="" v-model="order.quantity"></td>
     <td><input name="" v-model="order.price" disabled></td>
     <td><input name="" v-model="order.quantity * order.price"></td>
 </tr>

To display the product name from the product table where it says PRODUCT NAME, you can use both the Product and Order models available.

Answer №1

Make sure to preload the association:

return response()->json([ 'orders' => $order->load('product') ]);

Once that is done (assuming the order is linked to a product), you can display the product name in Vue using this syntax:

{{ order.product.name }} 

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

The correct method for concealing components from unauthorized users in Vue

After much thought, I'm considering transitioning my app entirely to Vue frontend. However, there are some concerns on my mind, such as: Currently, in Laravel blade (posts page), I have the following structure: @foreach($posts as $post) <post dat ...

The map fails to load on the HTML page

I am struggling to load a map into my HTML page from a file located in the app folder. Despite using the correct code for inserting the map, it still does not load properly. <!-- Contact section start --> <div id="contact" class="contact"> ...

Retrieve an entire item from a single data point

I am currently developing a ticket system and I am looking to implement a feature that allows users to close a ticket, removing it from their account. In my MongoDB array, the structure of the tickets is as follows: { "tickets": [{ &q ...

problems encountered while trying to increment ng-click function in Angular JS and Ionic

I'm currently working on developing a quiz using Angular JS and Ionic Framework. However, I've encountered an issue: The "Continue" button is not functioning as expected when clicked (using ng-click) to move to the next question. &l ...

What could be causing the sorting function to malfunction on certain columns?

My table's column sorting feature works well with first name and last name, but it seems to have issues with dl and dl score columns. I need assistance in fixing this problem. To access the code, click here: https://stackblitz.com/edit/angular-ivy-87 ...

Utilizing Backbone script for fetching data from an external JSON source

I am seeking assistance on how to utilize a Backbone script to display data from a JSON file. Here is the structure of the HTML Page: <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</tit ...

Having trouble processing images in multi-file components with Vue and TypeScript

Recently, I reorganized my component setup by implementing a multi-file structure: src/components/ui/navbar/ Navbar.component.ts navbar.html navbar.scss Within the navbar.html file, there was an issue with a base64-encoded image <img /> ...

Retrieve duplicate keys from a nested JSON file and store them in a dictionary as a list

I am facing an issue with a JSON file that is structured like this: { "details": { "hawk_branch": { "tandem": { "value": "4210bnd72" } }, "uclif_branch": { "tandem": { "value": "e2nc712nma89", "valu ...

How can you personalize a website script by deactivating it using uBlock Origin and then reintegrating it as a userscript?

Can you imagine if it were possible to address a problematic portion of a script on a website by preventing the original script from loading, copying the source code, editing it, and then re-injecting it as a userscript with Tampermonkey? I attempted this ...

Despite declaring a default export, the code does not include one

Software decays over time. After making a small modification to a GitHub project that was three years old, the rebuild failed due to automatic security patches. I managed to fix everything except for an issue with a default import. The specific error mess ...

Vue.js validation using vee-validate encountering an issue with errors

I am attempting to implement validation in Vue.js using vee-validate The version of Vue.js I am working with is 2.6.10 To install, I ran -npm install vee-validate In my main.ts file: import VeeValidate from "vee-validate"; Vue.use(VeeValidate); Howeve ...

Can conditional statements be utilized within a React component?

Using Material UI, the CardHeader component represents the top part of a post. If the post is created by the user (isUser = true), I would like to display two buttons on the post. Is this achievable? <CardHeader avatar={ <Avatar sx={{ ...

Visual Latency when Quickly Toggling Between Images in Succession

I have a series of 50 images that need to be displayed sequentially inside a div. The time interval between displaying each image is initially set at 750 milliseconds and decreases with each subsequent image. To ensure all images are loaded before the an ...

Adjust image size based on the size of the screen

I am facing an issue with two images that are created for a 1024 x 768 resolution but I need to use them in a higher resolution. The challenge is to align these two images so that the circle from the first image falls between the second image. Although I ...

Pug template syntax for importing JavaScript files with links

My Programming Dilemma In my NodeJS webserver setup, I use Express and Pug to serve HTML and JavaScript files. Here's a snippet of how it looks: index.pug html head title Hello body h1 Hello World! script(src='s ...

Adding an element to a blank array using Angular

When attempting to add a new item to an empty array, I encounter an error: $scope.array.push is not a function. However, when the array is not empty, the push operation works flawlessly and updates my scope correctly. Just so you know, my array is initia ...

A React component that exclusively renders component tags

After successfully loading index.html with no JavaScript errors, I ran into an issue where nothing was rendering on the page. Upon inspecting the webpage, all I could see was a tag and nothing else. It turns out that I have a component called "list". The ...

In the realm of React Native, an error arises when attempting to access 'this._nativeModule.isBluetoothEnabled', as the reference to null prevents it from

Recently, I delved into working with react native and attempted to incorporate react-native-bluetooth-classic into my project. The URL for the API overview can be found here. However, I encountered an issue that has me stuck: "TypeError: null is not ...

Creating a personalized cover for devextreme column in datagrid: A Step-by-Step Guide

I have encountered an issue with wrapping a Column inside my DataGrid. My goal is to create a customized component that generates a Column with the correct formatting. For instance, I want to develop a ColumnDate component that includes specific date forma ...

Improve Email Regular Expression to Restrict Consecutive Periods

I'm not very good with regular expressions, so I decided to seek some help. Here's the regular expression I have: /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.) ...