Meeraj Adhikari
Meeraj Adhikari

Reputation: 391

Save multidimensional array using Laravel and ajax

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.

Upvotes: 1

Views: 358

Answers (2)

STA
STA

Reputation: 34668

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']);
}

Upvotes: 0

Ozan Kurt
Ozan Kurt

Reputation: 3867

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);
}

Upvotes: 1

Related Questions