boill30's avatar

how to retrieve the product id based on the selected id_item

public function store(Request $request)
    {
        DB::beginTransaction();
        try {
            $save = new Service;
            $save->name = $request->name;
            $save->product_treatment_code = $request->product_treatment_code;
            $save->description = $request->description;
            $save->save();

            $items   = [];
            foreach ($request->items as $r) {
                $dummy  = new ServiceDetail;
                $dummy->item_id     = $r['item_id'];
                $dummy->product_id  = $r['product_id'];
                $dummy->expire_date = $r['expire_date'];
                $dummy->uom_id      = $r['uom_id'];
                $dummy->price       = $r['price'] ? $r['price'] : 0;
                $dummy->qty         = $r['qty'] ? $r['qty'] : 0;

                $items[] = $dummy;
            }
            $save->service_details()->saveMany($items);
            DB::commit();
        }catch(\Exception $e){
            DB::rollback();

            return response()->json([
                'code' => 500,
                'error' => 'Can not save the data'
            ], 500);
        }
    }
 $product = Item::where('id_item', $id)->where('id_item', $id)->firstOrFail();

so automatically retrieve product id data when any items that I choose and input into the column array

0 likes
2 replies
bobbybouwmann's avatar

Well, your query already looks correct. I'm not sure what you're trying to do here. Maybe you can tell us more about how your database is set up and what kind of data you have in there.

Also, your query is correct but you have a double where which is not needed

 $item = Item::where('id_item', $id)->firstOrFail();
boill30's avatar

can give me an example to apply it ?

foreach ($request->items as $r) {
                $dummy  = new ServiceDetail;
                $dummy->item_id     = $r['item_id'];
                $dummy->product_id  = $r['product_id'];
                $dummy->expire_date = $r['expire_date'];
                $dummy->uom_id      = $r['uom_id'];
                $dummy->price       = $r['price'] ? $r['price'] : 0;
                $dummy->qty         = $r['qty'] ? $r['qty'] : 0;

                $items[] = $dummy;
            }

for get this from item model

$dummy->product_id  = $r['product_id'];

Please or to participate in this conversation.