jeudi 22 juin 2017

Insert Multiple Row From View Using Eloquent Laravel 5

How can I insert Multiple Row of Cart:: content() in Laravel 5.
I'm using Crinsane/LaravelShoppingcart for my cart, however, when you click on proceed to payment, I want the user to fill Customer details like (Terminal ID, sale date, and customer name. But I'm having issue with inserting Cart::(Content) into my database (I dont want to insert instance. I like to break the cart down to my database column. example, if someone pick like 2-5 items, I want it to be inserted to my db, with each item in each row. I've break the Cart::content down in my view, but I need to be able to use Laravel to insert multiple row based on the number of Items selected. PLEASE NOTE: IF I ONLY ADD ONLY ONE ITEM TO CART, I'M ABLE TO INSERT TO DATABASE, IT'S ONLY WHEN THE ITEM IN CART IS MORE THAN ONE THAT I HAVE ISSUE WITH. Below is my proceed.blade.php

 <div class="panel-heading">Customer Detail</div>
    <div class="panel-body">
       @if (sizeof(Cart::content()) > 0)
    {!! Form::open(['method'=>'POST', 'action'=> 'SalesController@store','files'=>true]) !!}
       {!! Form::text('counter', $counter=Cart::content()->groupBy('id')->count(),  ['class'=>'form-control'])!!}
        @foreach (Cart::content() as $item)
        <div class="form-group">
   {!! Form::label('product_name', 'Product Name:') !!}
  {!! Form::text('product_name', $item->name,  ['class'=>'form-control'])!!}                     </div>
          <div class="form-group">
    {!! Form::label('quantity', 'Quantity:') !!}
  {!! Form::text('quantity', $item->qty,  ['class'=>'form-control'])!!}
         </div>
       <div class="form-group">
  {!! Form::label('unit_price', 'Unit Price:') !!}
  {!! Form::text('unit_price', $item->price,  ['class'=>'form-control'])!!}
                     </div>
       <div class="form-group">
   {!! Form::label('rowId', 'Row ID:') !!}
   {!! Form::text('rowId', $item->rowId,  ['class'=>'form-control'])!!}
       </div>
    <div class="form-group">
           {!! Form::label('tax', 'Tax:') !!}
        {!! Form::text('tax', $item->tax,  ['class'=>'form-control'])!!}
           </div>
            <div class="form-group">
      {!! Form::label('total_price', 'Sub Total:') !!}
      {!! Form::text('total_price', $item->subtotal,  ['class'=>'form-control'])!!}
           </div>
          @endforeach

                     @endif
         <div class="form-group">
       {!! Form::label('terminal_id', 'Terminal ID:') !!}
       {!! Form::text('terminal_id', null, ['class'=>'form-control'])!!}
                       </div>
                       <div class="form-group">
       {!! Form::label('customer_name', 'Customer Name:') !!}
   {!! Form::text('customer_name', null,  ['class'=>'form-control'])!!}
                        </div>
                      <div class="form-group">
          {!! Form::label('sale_date', 'Sale Date:') !!}
         {!! Form::text('sale_date', null, ['class'=>'form-control'])!!}
                        </div>
                        <div class="form-group">
          {!! Form::submit('Check Out', ['class'=>'btn btn-primary']) !!}
                       </div>
         {!! Form::close() !!}
`

MY CONTROLLER

$counter= $request['counter'];
    for ($i = 0; $i < count($counter); $i++) {
Sale::create([
             'terminal_id' => $request['terminal_id'],
             'customer_name' => $request['customer_name'],
             'unit_price' => $request['unit_price'],
             'total_price' => $request['total_price'],
             'tax' => $request['tax'],
             'quantity' => $request['quantity'],
             'product_name' => $request['product_name'],
             'sale_date' => $request['sale_date'],
             'rowId' => $request['rowId']
    ]);

    }
    return redirect('shop');
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2s0xiRL
via IFTTT

Aucun commentaire:

Enregistrer un commentaire