本文介绍了Laravel |更新查询返回"[{"laboratory":"Boulanger"}]"作为价值而非“富兰格"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的一个控制器中创建了一个钩子函数,用于在提交表单后更新字段.

I have created a hook function in one of my controller to update a field after submitting the form.

该钩子应使用实验室名称填充"orders_detail"数据库的实验室"字段.

The hook should populate the field "laboratory" of the "orders_detail" database with the name of a laboratory.

/* POPULATE the laboratory FIELD */
DB::table('orders_detail')->where('id',$id)->first();
/* dd($id); */

/* REQUESTED VALUE IN ARRAY :   "ordersdetail-productname" => array:1 [▼ 0 => "1" ] : */

$productname = $_REQUEST['ordersdetail-productnameID'][0];
/* dd($productnameID); **RETURN VALUE "1"** */

/* QUERY : select laboratories.laboratory FROM products LEFT JOIN laboratories ON products.laboratoryID = laboratories.id WHERE products.id = productnameID : */

$laboratory = DB::table('products')
                ->join('laboratories', 'products.laboratoryID', '=', 'laboratories.id')
                ->selectRaw('laboratories.laboratory')
                ->where('products.id', '=', $productnameID)
                ->get();

/* dd($laboratory); **RETURNED ARRAY 
        Collection {#3536 ▼
          #items: array:1 [▼
            0 => {#3534 ▼
              +"laboratory": "Boulanger"
            }
          ]
        }** */

/* UPDATE THE laboratory FIELD WITH THE RETURNED VALUE */
DB::table('orders_detail')->where('orderID', '=', $id)
->update(['laboratory'=> $laboratory]);

我如何摆脱实验室":"Boulanger" ,而保留 Boulanger ,这是我要更新"laboratory"字段的实验室名称

How can I get rid of "laboratory": "Boulanger" and just keep Boulanger which is the name of the laboratory I want to update the field "laboratory" with.

感谢您的专业知识.

干杯,马克

推荐答案

将您的实验室查询更新为

$laboratory = DB::table('products')
                ->join('laboratories', 'products.laboratoryID', '=', 'laboratories.id')
                ->selectRaw('laboratories.laboratory')
                ->where('products.id', '=', $productnameID)
                ->first();

然后$laboratory->laboratory将给您期望的值.

更新为

/* UPDATE THE laboratory FIELD WITH THE RETURNED VALUE */

if($laboratory->laboratory) {
    DB::table('orders_detail')
       ->where('orderID', $id)
       ->update(['laboratory'=> $laboratory->laboratory]);
}

这篇关于Laravel |更新查询返回"[{"laboratory":"Boulanger"}]"作为价值而非“富兰格"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 23:29