После этого предыдущего вопроса , где я забыл указать, что массив может возвращать несколько продуктов (productnameID), которые могут быть произведены различными лабораториями, но которое обновляло поле 'лаборатория' таблицы 'orders_detail'с первой лабораторией, возвращенной запросом.Теперь я изменил несколько вещей:
**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 (я полагаю).Заранее спасибо, ура, Марк