1

**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');
9
  • Please add the route code also if possible Commented Aug 14, 2020 at 5:22
  • @Amit Patel route code updated Commented Aug 14, 2020 at 5:31
  • @Amit Patel route code updated please help me Commented Aug 14, 2020 at 5:32
  • 1
    @user12931578 Then you didn't read any documentation or did any research effort by yourself. It is not acceptable that someone should write your whole codebase Commented Aug 14, 2020 at 5:56
  • 1
    @Aless55 i did research. if i post my controller code it will confuse my codes for everyone so i was not updated controller , if you have solution for this please help, #motivate if anyone learning something new if you have knowledge share with everyone. please Commented Aug 14, 2020 at 6:09

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

15 Comments

Thank you For your help. every thing working fine except ItemList not updating perfectly it just storing one itme every time i submitting a form. my form passing more then 2 items every submission but it storing one value.
@user12931578 hmm that is strange, if the other 2 work, then the third one should also work. I changed ItemList() to ItemList it was a typo by me, maybe this resolves the problem.
if i use this ItemList() it showing error like this, Call to undefined function App\Http\Controllers\ItemList(). if i use ItemList its storing only one value
@user12931578 yes then thats not the problem. Are you sure that the closing } is after all create statements?
BillProduct and ItemList also not storing multiple values yes i checked } closed
|

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.