Laravel |查询|更新具有相同orderID和不同producttypeID的行

时间:2018-12-04 12:49:46

标签: laravel-5 pdo eloquent

在之前的question之后,我忘记指定该数组可以返回可以由不同实验室生产的几种产品(productnameID),但该产品使用表更新了“ orders_detail”表的“ laboratory”字段查询返回的第一个实验室。现在我已经更改了几件事:

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

/* QUERY TO BIND THE PRODUCTTYPEID TO A LABORATORY : select laboratories.laboratory FROM orders_detail LEFT JOIN laboratories ON orders_detail.producttypeID = laboratories.id where orders_detail.orderID = $rowID | Instead of the productnameID I'm using now the producttypeID which is appropriate. Eacch producttype belongs to one unique laboratory  */

$laboratory = DB::table('orders_detail')
                ->join('laboratories', 'orders_detail.producttypeID', '=', 'laboratories.id')
                ->select('laboratories.laboratory')
                ->where('orderID', '=', $id)
                ->get();

dd($laboratory) generates following :
Collection {#3584 ▼ #items: array:2 [▼ 0 => {#3581 ▼ +"laboratory": "Cuisine" } 1 => {#3582 ▼ +"laboratory": "Boulanger" } ] }

I have created an order with 2 products so I get the appropriate laboratory for each product "laboratory": 

"Cuisine" and "laboratory": "Boulanger" even if I have here a strange format with "laboratory": which I don't need, same for the double quotes wrapping the laboratory name.

Now I need to update my two rows with the same orderID:

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

But of course with the above actual query I get this :

id  | orderID | producttypeID | productnameID | laboratory
225 | 206     | 4             | 26            | [{"laboratory":"Cuisine"},{"laboratory":"Boulanger"}] 
226 | 206     | 1             | 1             | [{"laboratory":"Cuisine"},{"laboratory":"Boulanger"}]

Instead of this :

id  | orderID | producttypeID | productnameID | laboratory
225 | 206     | 4             | 26            | Cuisine 
226 | 206     | 1             | 1             | Boulanger

I need now being able to assign the appropriate laboratory to to the appropriate row :

Product '26' belongs to producttypeID '4' and producttypeID '4' is produced in the laboratory 'Cuisine'
Product '1' belongs to producttypeID '1' and producttypeID '1' is produced in the laboratory 'Boulanger'

Table 'laboratories' :

id  | laboratory
1   | Boulanger
2   | Chocolat
3   | Magasin
4   | Cuisine

"Rule" is simple : productypeID = laboratories.id, so if producttypeID is 4 than this producttype is produced in laboratory 4 which is Cuisine.

由于我遇到了麻烦,并且我不知道如何使用foreach循环构建更新查询(我想),因此请再次感谢您的专业知识。预先感谢您,马克,马克

0 个答案:

没有答案