I'm using DarrylCode's ShoppingCart package and I'm having a problem when adding to cart, because for some reason it doesn't add the first product in the table, but when I add another from any other position it lets me add it.
My add function:
public function add(Request $request){
$producto = Producto::find($request->id);
Cart::add(
$producto->id,
$producto->descripcion,
$producto->quantity = 1,
);
return back();
}
My table:
<table id="productosexistentes" class="table table-striped table-bordered shadow-lg nt-4">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Imagen</th>
<th scope="col">Código</th>
<th scope="col">Descripción</th>
<th scope="col">Stock</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
@foreach ($productos as $producto)
<tr>
<td>{{$producto->id}}</td>
<td>
<img src="/imagen/{{$producto->imagen}}" style="height: 50px; width: 50px; border-radius: 20%;">
</td>
<td>{{$producto->codigo}}</td>
<td>{{$producto->descripcion}}</td>
<td>{{$producto->cantidad}}</td>
<td>
<form action="{{route('cart.add')}}" method="POST" enctype="multipart/form-data">
@csrf
<input type="hidden" value="{{ $producto->id }}" name="id">
<input type="hidden" value="{{ $producto->descripcion }}" name="descripcion">
<input type="hidden" value="{{ $producto->cantidad }}" name="cantidad">
<input type="hidden" value="{{ 1 }}" name="quantity">
<div class="flex justify-center rounded-lg" role="group">
<button class="px-4 py-2 text-white bg-blue-800 rounded">Agregar</button>
</div>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
I only have that problem with the first data, and I don't understand what could be happening to me. I have tried to delete it and see if it was an error with that product, but it does not add me it is the first product that is in the table.
When I hit the add button, it doesn't do anything for me, but only in the first data. If I add another one and then try to add the first one it does let me add it.
I'm glad it worked for you, here's how I explain it to you:
By having a comma (,) in
$producto->quantity = 1,
it expecting one more parameter, the function will probably execute but with errors.The same applies to an SQL query : For example:
"SELECT id, descripcion, quantity, FROM table"
said statement is wrong again because of the comma (,) only in SQL the query will not be executed and you will probably get an error.Cheers!