th tom
th tom

Reputation: 1

how to store an array value from blade to database in Laravel

submitting a dynamic form but can't store the value to database. im a beginner i stuck for 2,3 days but cant solve the problem.

the request value is like {"_token":"PTGmyf3UZrD1TgKDJcREcKia9VFRLYaP5kNaAkAU","id":["28","31"],"name":["Fogg","zack"],"rate":["70","5000"],"quantity":["1","1"],"submit":"Submit"}


  <form name="" id="form" action="{{ url('/store_order_item') }}" method="POST">  
  @csrf
<table class="table table-bordered" id="dynamic_field">
 <thead>
   <tr>
     <th>Product Id</th>
     <th>Name</th>
     <th>Rate</th>
     <th>Quantity</th>
     <th>Amount</th>
     <th>Action</th>
   </tr>
 </thead>

  <tbody>
    
  </tbody>
    
</table> 
<h6>Grand Total: <span id= "grandtotal"></span> </h6>
<input type="button" name="" id="" class="btn btn-sm btn-danger" value="Cancel">  
<input type="submit" name="submit" id="submit" class="btn btn-sm btn-secondary" value="Submit"/>  

</form>  
</div>

<script type="text/javascript">

  var i = 1;
  
    $(document).ready(function(){
    
      var button_id1 ;

    $('#button').click(function(){

  let namesearch = $('#search').val();
 
  let idsearch = $('#idsearch').val();

  if ((namesearch == '')&&(idsearch == '')){
      return false;
    }

  $.ajax({
      type:"GET",
      url: "/pos_search_ajax",
      data: { name: namesearch, id: idsearch },
    
      success: function(response) {
        $('#dynamic_field').append('<tr id="row_'+i+'"><td><input id="id_'+i+'" type="text" name="id[]" value="'+response[0].id+'" readonly></td><td><input id="name_'+i+'" name="name[]" type="text" value="'+response[0].name+'" readonly></td><td> <input id="rate_'+i+'" name="rate[]" type="text" value="'+response[0].price+'" readonly></td><td><input id="quantity_'+i+'" onchange="change('+i+')" name="quantity[]" type="number" min=1 value="1"></td><td class="total" id="total_'+i+'" name="total[]">'+response[0].price+'</td><td>  <button type="button" onclick="remove_tr('+i+')"  class="btn btn-danger btn-sm">Delete</button></td></tr>'); 
        
        total();
       
        i++; 
      }   
  }); 

# this is the Controller #

public function store(Request $request)
    {  
        foreach($request->all() as $itm)
        {
            $items = New OrderItem();
            $items->product_id = $request->id;
            $items->name = $request->name;
            $items->save();
        }
   }

Upvotes: 0

Views: 586

Answers (2)

TheArKa
TheArKa

Reputation: 329

In case your request always formatted like that, then using foreach($request->all() as $itm) will only return what you send, value of id will be ["28","31"] and so on. So you could try something like this:

$data = $request->all();
for ($i=0; $i < count($data['id']); $i++) { 
    $item = new OrderItem();
    $item->product_id = $data['id'][$i];
    $item->name = $data['name'][$i];
    $item->save();
}

That code will iterate over an id, and the product name must be in the same order as the id.

Upvotes: 2

Meow Kim
Meow Kim

Reputation: 484

Multiple request parameter with same name needs additional array symbol([]) on its name attirbute, and u did it right way like <input name="name[]" ....
This makes parameter that is array. (not a single value)

So u when u access the parameter, u need to access 1 more depth using index number(to get nth item).
To access all the items, u need to loop that parameter.

And parameters is not only one(id, name, rate, quantity), it is need that a parameter to be basis to loop.
The basis parameter could be anything, but normally identifiers preferred. (eg, id)

And the code to write ur own is something like @TheArka's answer. But recommend u know how's going on, why does it do.

Upvotes: 0

Related Questions