Good evening I'm working with laravel 5.4 the problem is that I have two tables an order and detail of order I made a shopping cart is all good with session to what I'm going is that when I want to insert the order table and order detail does not insert. I leave the code there, Any contribution is welcome thanks in advance. PS: I'm with mysql
this is the driver:
public function postgrabardetalle(Request $request)
{
try {
DB::beginTransaction();
$pedido=new Pedido;
$pedido->cod_cliente=$request->get('cod_cliente');
$mytime=Carbon::now('America/Lima');
$pedido->fecha=$mytime->todateTimeString();
$pedido->igv='18';
$pedido->cod_empleado=1;
$pedido->estado='1';
$pedido->save();
$detpedido=new DetallePedido;
$cod_producto=$request->input('cod_producto');
$cantidad=$request->input('cantidad');
$precioventa=$request->input('precioventa');
$importe=$request->input('importe');
//$cart=new Carro($oldCart);
$contador=0;
while ($contador < count($cod_producto)) {
$detpedido->cod_pedido=$pedido->cod_pedido;
$detpedido->cod_producto=$cod_producto[$contador];
$detpedido->cantidad=$cantidad[$contador];
$detpedido->precioventa=$precioventa[$contador];
$detpedido->importe=$importe[$contador];
$detpedido->save();
$contador=$contador+1;
}
DB::commit();
} catch (Exception $e) {
DB::rollback();
}
Session::forget('carro');
return Redirect::to('/carro')->with('success','pedido agregado correctamente');
//return $cod_producto;
}
this is the view
-----------------------------------
@extends('layouts.inicio')
@section('content')
@if(Session::has('carro'))
<form action="{{route('grabardetalle')}}" method="POST">
<div class="row">
<div class="col-sm-6 col-md-6 col-md-offset-1 col-sm-offset-1">
<label for="cliente">Clientes</label>
<select name="cod_cliente" id="" class="form-control selectpicker" data-Live-search="true">
@foreach($clientes as $cli)
<option value="{{$cli->cod_cliente}}">{{$cli->nombre}}</option>
@endforeach
</select>
</div>
<div class="col-sm-6 col-md-6 col-md-offset-1 col-sm-offset-1">
<h3 class="col-sm-offset-4">Detalle de Pedido</h3>
<br>
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th class="info">Descripcion</th>
<th class="info">Cantidad</th>
<th class="info">Precio</th>
<th class="info">Importe</th>
<th class="info">Acciones</th>
</tr>
</thead>
<tbody>
@foreach((array)$producto as $pro)
<tr >
<td style=""><input type="hidden" name="cod_producto[]" value="{{$pro['item']['cod_producto']}}" disabled=""> {{$pro['item']['descripcion']}} </td>
<td> <input type="text" name="cantidad[]" value="{{$pro['cant']}}" disabled="" > </td>
<td><input type="text" name="precioventa[]" value="{{$pro['precioventa']}}" disabled="" > </td>
<td><input type="text" name="importe[]" value="{{$pro['importe']}}" disabled=""> </td>
<td width="50" ><a href="{{ route('carro.reducituno', ['id' => $pro['item']['cod_producto']])}}" type="button" class="btn btn-danger">Elimnar 1</a>
<a href="{{ route('carro.removeItem', ['id' => $pro['item']['cod_producto']])}}" type="button" class="btn btn-primary">Limpiar</a></td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
<div class="row">
<div class="col-sm-6 col-md-6 col-md-offset-1 col-sm-offset-1">
<h3>Total: S/. <input type="text" name="" value="{{$subtotal}}" disabled=""> </h3>
</div>
</div>
<hr>
<div class="row">
<div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
{{ csrf_field() }}
<button type="submit" class="btn btn-success" name="">Grabar</button>
<button type="reset" class="btn btn-danger" name="">Cancelar</button>
</div>
</div>
</form>
@else
<div class="row">
<div class="col-sm-6 col-md-6 col-md-offset-3 col-sm-offset-3">
<h2>No Hay Productos Agregados</h2>
</div>
</div>
@endif
@endsection