【问题标题】:I have product_id inside line_items array in order table, I want to show product name against this product_id in laravel using vue.js我在订单表中的 line_items 数组中有 product_id,我想使用 vue.js 在 laravel 中针对此 product_id 显示产品名称
【发布时间】:2019-06-21 11:47:48
【问题描述】:

我在订单表中有一个名为 line_items 的单元格。在 line_items 里面有类似的数据

  [
      {"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"}
    ]

我有产品表,其中产品的名称存储在名为名称的单元格中。 products表的id就是上面数据中的product_id。

现在我有一条类似的路线

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

这是ReturnController中的json函数

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

这是http://127.0.0.1:8000/json/7路由中的json输出

{"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"}}

这是vue中的代码

 <tr v-for="order in orders.line_items">
     <td><input name="" v-model="order.product_id"></td>
     <td><input name="" v-model="PODUCT 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>

我想从写有 PRODUCT NAME 的产品表中显示产品名称。怎么做。

我有 Product 和 Order 模型。

【问题讨论】:

    标签: javascript mysql json laravel vue.js


    【解决方案1】:

    尝试预先加载关系:

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

    之后(假设订单属于产品),您可以在Vue中显示产品名称如下:

    {{ order.product.name }} 
    

    【讨论】:

    • 这些是我在订单模型中添加的代码,但没有得到预期的结果。 public function product() { return $this->belongsTo(Product::class); }
    • 它不起作用,请帮助我。在控制台中收到此错误。 TypeError:“order.product 未定义”
    • 先检查json路由/json/7是否包含产品。
    • 不,json路由中没有产品!
    猜你喜欢
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 2018-03-17
    相关资源
    最近更新 更多