mardi 4 décembre 2018

Update query with data from array

Following this previous question where I forgot to specify that the array could return several products (productnameID) which can be produced by different laboratories, but which was updating the field 'laboratory' of table 'orders_detail' with the first laboraty returned by the query :

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

/* REQUESTED VALUE IN ARRAY */
$productname = $_REQUEST['ordersdetail-productnameID'][0];

/* QUERY TO BIND THE PRODUCTNAMEID TO A LABORATORY */

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

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

I need being able to assign different laboratories depending the 'producttypeID' which is available into the submitted data :

$productname = $_REQUEST['ordersdetail-productnameID'];
    dd($productnameID);

return the array containing the 2 productnameIDs :

array:2 [▼
  0 => "26"
  1 => "1"
]

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

Table 'orders_details field 'laboratory' should be populated as followed :

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

and not like it is now the case with the previous   :

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

Now I would need to populate the field 'laboratory' according to following rule :

orders_detail.producttypeID = laboratories.id and populate the field 'orders_detail.laboratory' with the values of 'laboratories.laboratory'.

Table 'laboratories' :

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

Would appreciate your expertise here again. Thanks in advance, cheers, Marc



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Qf5k5J
via IFTTT

Aucun commentaire:

Enregistrer un commentaire