1

I am building a pharmacy management system. I have a condition where I need to build a section enter image description here

As shown in the picture, I need to store the data where the upper three should be same for all of the rows inputted below. The form data is submitted as in the below picture.

enter image description here

But the data when looped and saved is not being saved as desired. Only the last row of the data is being inserted and I am also confused to store supplier, purchase date and note since these data should be repeated as many as the number of rows are added.

PurchaseController.php

public function storePurchase(PurchaseStoreRequest $request)
{
   
    $purchase = new Purchase();

    $count = count($request->name);

    // return response()->json($request->all());

    for ($i = 0; $i < $count; $i++) {

        $purchase->supplier = $request->supplier;
        $purchase->purchase_date =  $request->purchase_date;
        $purchase->description = $request->description;

        $purchase->category = $request->category[$i];
        $purchase->name = $request->name[$i];
        $purchase->generic_name = $request->generic_name[$i];
        $purchase->batch_number = $request->batch_number[$i];
        $purchase->company = $request->company[$i];
        $purchase->strength = $request->strength[$i];
        $purchase->expiry_date = $request->expiry_date[$i];
        $purchase->quantity = $request->quantity[$i];
        $purchase->selling_price = $request->selling_price[$i];
        $purchase->purchase_price = $request->purchase_price[$i];

        $purchase->save();

    }

    return response()->json(['message'  =>  'Purchase Saved Successfully']);
}

Can someone help me to store these three fields in the database repeating the number of rows submitted ? Currently only the last row is being inserted into the database.

2
  • $purchase = new Purchase(); write this line inside the loop, you did it outside. Thats whay only last low inserted. Anyway, its will $purchase = new Purchase; Commented Oct 24, 2020 at 5:16
  • have you tried array_merge_recursive()? Commented Oct 24, 2020 at 5:31

2 Answers 2

1

This might be an another way to accomplish what you're looking for.

$sharedKeys = ['supplier', 'purchase_date', 'description'];
$sharedData = $request->only($sharedKeys);

$multiKeys = ['category', 'name', 'generic_name', 'batch_number', 'company', 'strength', 'expiry_date', 'quantity', 'selling_price', 'purchase_price'];
$multiData = $request->only($multiKeys);

for ($i = 0; $i < count($request->name); $i++) {
    $individualData = array_combine($multiKeys, array_column($multiData, $i));

    Purchase::create($sharedData + $individualData);
}
Sign up to request clarification or add additional context in comments.

Comments

0

For every loop, you need to create a new instance, then It will create a new record on each loop :

public function storePurchase(PurchaseStoreRequest $request)
{
    // $purchase = new Purchase();

    $count = count($request->name);

    // return response()->json($request->all());
    for ($i = 0; $i < $count; $i++) {
        $purchase = new Purchase();  // create new instance end with (); on each loop
        $purchase->supplier = $request->supplier;
        $purchase->purchase_date =  $request->purchase_date;
        $purchase->description = $request->description;
        $purchase->category = $request->category[$i];
        $purchase->name = $request->name[$i];
        $purchase->generic_name = $request->generic_name[$i];
        $purchase->batch_number = $request->batch_number[$i];
        $purchase->company = $request->company[$i];
        $purchase->strength = $request->strength[$i];
        $purchase->expiry_date = $request->expiry_date[$i];
        $purchase->quantity = $request->quantity[$i];
        $purchase->selling_price = $request->selling_price[$i];
        $purchase->purchase_price = $request->purchase_price[$i];
        $purchase->save();
    }
    return response()->json(['message' => 'Purchase Saved Successfully']);
}

3 Comments

Thank you for your response. I was wondering why the data was not submitted. And just pushing that line inside did the trick.
Can you share me any idea to store total of the purchase without having another table to do so? I mean total of every rows (Quantity * CP)
Do you want to store Quantity * CP on another table?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.