user12931578
user12931578

Reputation: 49

laravel 7 store multiple values in different row and also another tables in different row

**help me? I want to input multiple value and store in database with different row. with some other values inset in anther 2 different tables at same time. im not able to code controller store part i spent 3days help me **

example :

In input form, to "table name"( Purchases )

    |   product   quantity     price     manufacturer    |
    |     a           1         12$           xyz        |
    |     b           2         1$             x         |
    |     c           10        10$            y         |

also insert selected fled 'product' 'quantity' 'price' this value to "table name"( bill_products)

    |   product   quantity     price    |
    |    a           1         12$      |
    |    b           2         1$       |
    |    c           10        10$      |

also insert selected fled 'product' 'quantity' this value to "table name"( item_lists)

    |   product   quantity     |
    |    a           1         |
    |    b           2         |
    |    c           10        |

This is my view (purchase\purchase-entry.blade.php)

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">PURCHASE FORM</div>

                <div class="card-body">
            
                @if (Session::has('success'))
                    <div class="alert alert-success">{!! Session::get('success') !!}</div>
                @endif
                @if (Session::has('failure'))
                    <div class="alert alert-danger">{!! Session::get('failure') !!}</div>
                @endif
                         
    <form method="POST"   action="{{route('purchasesave')}}" enctype="multipart/form-data">
 @csrf
<div class="form-row">

    <div class="col-md-4 mb-3">
      <label >product NAME</label>
      <input type="text"  name="product[]"  placeholder="product" value="" required >
    </div>
    <div class="col-md-4 mb-3">
      <label >quantity</label>
      <input type="text"  name="quantity[]"  placeholder="quantity" value="" required >
    </div>
    <div class="col-md-4 mb-3">
      <label >price</label>
      <input type="text"  name="price[]"  placeholder=price" value="" required >
    </div>
    <div class="col-md-4 mb-3">
      <label >manufacturer</label>
      <input type="text"  name="manufacturer[]"  placeholder="manufacturer" value="" required >
    </div>
  </div>
</div>
<button class="btn btn-primary float-right" type="submit">SAVE</button>
</form>

This is my controller (PurchaseController.php) and I have 3 model (Purchase , Bill_product and Item_list )

public function purchasesave(Request $request)
   {

   }

this is route part

Route::post('purchasesave', 'PurchaseController@purchasesave')->name('purchasesave');

Upvotes: 1

Views: 874

Answers (1)

Aless55
Aless55

Reputation: 2709

Since you are passing an array for each attribute, you will be able to iterate over it. Please make sure to properly valdiate your data. Afterwards you can simply get the items of each array at the same index. So for example all items at index 0 because they belong together.

public function purchasesave(Request $request)
   {
     //insert your validation here
    
     foreach($request->product as $k => $p){
       Pruchases::create([
           'product' => $request['product'][$k],
           'quantity' => $request['quantity'][$k],
           'price' => $request['price'][$k],
           'manufacturer' => $request['manufacturer'][$k],
       ]);

       BillProduct::create([
           'product' => $request['product'][$k],
           'quantity' => $request['quantity'][$k],
           'price' => $request['price'][$k],
       ]);
 
       ItemList::create([
           'product' => $request['product'][$k],
           'quantity' => $request['quantity'][$k],
       ]);
     }
   }

If you don't use models you can simply replace the statements by using DB and insert(), to insert the data into your DB.

Upvotes: 1

Related Questions