obink's avatar
Level 1

retrieve a single column with a peculiar ID from a database and put it in at a different table

I think this is a PHP confusion, but I put it in Laravel because I try to learn it ATM.

this is what I tried

invoiceController

public function store(Request $request)
    {
        $invoice = invoice::create($request->all());
        $products = $request->input('products', []);
        $quantities = $request->input('quantities', []);
        for ($product=0; $product < count($products); $product++) {
            $pricePoint = DB::table('products')->where('id', $products)->value('price');
            dd($pricePoint);
            if ($products[$product] != '') {
                $invoice->products()->attach([
                    $products[$product] => [
                        'quantity' => $quantities[$product],
                        'subtotal' => $quantities[$product]*$pricePoint,
                    ]
                ]);
        
Product::where('id', $products[$product])->decrement('qty',$quantities[$product]);
            }
        }
    
        return redirect()->route('productsOut.index');
    }

what I've been trying is to get the product's price that will be posted in invoice.create and try to put it in on order_product table as a subtotal value.

So it's like, I pick the product's price and paste it to get a value of subtotal = quantity * price to fill the subtotal column for each row.

$pricePoint = DB::table('products')->where('id', $products)->value('price');

this is what I tried to get the product's prices, I thought it work, but instead of retrieving 3 prices, it just a single price. which is the most top of the product I just store.

dd($products);

dd($pricePoint);

so the variable of $pricePoint I made is a flaw even it is inside the loop, it just retrieves a single price value Instead of retrieving 3 prices..

please help.

0 likes
5 replies
SilenceBringer's avatar
Level 55

Hi @obink try something like

public function store(Request $request)
    {
        $invoice = invoice::create($request->all());

        $inputProducts = $request->input('products', []);
        $inputQuantities = $request->input('quantities', []);

        $products = DB::table('products')->whereIn('id', $inputProducts)->get()->keyBy('id');

        foreach ($inputProducts as $index => $productId) {
            if ($products->has($productId) && Arr::has($inputQuantities, $index)) {
                $invoice->products()->attach([
                    $productId => [
                        'quantity' => $inputQuantities[$index ],
                        'subtotal' => $inputQuantities[$index] * $products->get($productId)->price,
                    ]
                ]);
        
                Product::where('id', $productId)->decrement('qty', $inputQuantities[$index]);
            }
        }
    
        return redirect()->route('productsOut.index');
    }
SilenceBringer's avatar

@obink and about your problem: value method returns specific value for the first item. You can grab the price for current product, but you'll need to use current product id

$pricePoint = DB::table('products')->where('id', $products[$product])->value('price');

or you pluck the result to have collection of product id => price pairs

$productPrices = DB::table('products')->whereIn('id', $products)->pluck('price', 'id');
obink's avatar
Level 1

Hi again @silencebringer and thanks again for answering my question, again.

I tried to understand what you were typed but I fail to understand.

this Arr:: something is new for me.

but then again, ty it works beautifully.

obink's avatar
Level 1

Hi, @sinnbeck Thank you for the link. I'll learn about this Arr::stuff .

Please or to participate in this conversation.