Laravel 5 Laravel| Update查询返回“[{”laboratory:“Boulanger”}]”作为值而不是“Boulanger”
我在我的一个控制器中创建了一个钩子函数,用于在提交表单后更新字段 钩子应使用实验室名称填充订单详细信息数据库的现场实验室Laravel 5 Laravel| Update查询返回“[{”laboratory:“Boulanger”}]”作为值而不是“Boulanger”,laravel-5,pdo,Laravel 5,Pdo,我在我的一个控制器中创建了一个钩子函数,用于在提交表单后更新字段 钩子应使用实验室名称填充订单详细信息数据库的现场实验室 /* POPULATE the laboratory FIELD */ DB::table('orders_detail')->where('id',$id)->first(); /* dd($id); */ /* REQUESTED VALUE IN ARRAY : "ordersdetail-productname" => array:1 [▼ 0
/* 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 = DB::table('products')
->join('laboratories', 'products.laboratoryID', '=', 'laboratories.id')
->where('products.id', '=', $productnameID)
->pluck('laboratories.laboratory');
将您的实验室查询更新为
$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]);
}
谢谢Prashant,实验室消失了,但我仍然有我想要摆脱的东西。表更新为:[Boulanger]。有什么线索吗?这就是Pull返回结果的方式。不是很确定,但试着用价值代替勇气。看看你是否得到了想要的结果。使用->首先而不是->获取;因为你在等一个实验室。那么dd$laboratory的输出是什么呢?@Ijas{3534▼ +laboratory:Boulanger}然后$laboratory->laboratory将为您的时间提供值Boulangerthanks,当我获得实验室值时,我将使用以下代码更新相应的行和列:DB::table'orders\u detail'->其中'orderID','=',$id->update['laboratory'=>$laboratory];我在哪里可以设置$laboratory->laboratory?非常感谢,它正在工作!你太棒了!但我忘了我们可以有不同的实验室现在根据订购的产品,我们可以有不同的实验室,例如:$productname=$\u REQUEST['ordersdetail-productnameID'];dd$productnameID;返回包含2个ProductNameID的数组:数组:2[▼ 0=>26 1=>1]这两种产品在不同的实验室生产,因此,我需要应用以下规则:表“实验室”:id |实验室1 | Boulanger 2 |巧克力3 | Magasin 4 |烹饪表“订单”|详细信息id |订单id |产品类型id |产品名称id |实验室225 | 206 | 4 | 26 |烹饪226 206 | 1 || Boulanger orders_detail.producttypeID=laboratories.id,并用“laboratory.laboratory”的值填充“orders_detail.laboratory”字段。如果上述问题得到解决,请您接受并询问一个新问题?然后可以单独解决。再次感谢您的时间和专业知识。如果您能看看以下问题,将不胜感激: