Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hikups's avatar

binding Array to extra fields on pivot table

I have 3 tables, a pedido table a product table and a pivot pedido_product. I try to update the cantidad field on the pivot table. I 'm able to with hard coded values (replied to me in this thread http://laracasts.com/discuss/channels/laravel/updating-data-on-the-pivot

$productId = 2; 
$cantidad = 1.1;
Pedido::find($pedidoId)->products()->updateExistingPivot($productId, [
"cantidad" => $cantidad
]);

Now i have a form where i try to update the cantidad on each product on the same request

 {!! Form::open(array('action' => ['PedidosController@update', $pedido->id],'method' => 'PATCH')) !!}
@foreach ($pedido->products as $product)
{!! Form::text($product->pivot->product_id, $product->pivot->cantidad, ['class' => 'form-control' ,'id' => 'cantidad']) !!}

i get what i want, an array with a key refrenced from the product_id and the value of the to be updated cantidad. but now how do i bind productId and cantidad in the update method to the array i recieve from my form and loop through them in my update method?

$pedido = $request->input();
$productId =  ? ;
$cantidad = ? ;
dd($productId);
Pedido::find($pedidoId)->products()->updateExistingPivot($productId, [
"cantidad" => $cantidad

Thanks

0 likes
1 reply
hikups's avatar
hikups
OP
Best Answer
Level 1

I didnn't found the ansert but i changed the approach, instead of updating them all at the same time, i update them one by one by opening the form after the loop and passing throug a hidden form that provides the product_id

this does the trick

@foreach ($pedido->products as $product)
       {!! Form::open(array('action' => ['PedidosController@update', $pedido->id],'method' => 'PATCH')) !!}
             {!! Form::text('cantidad', $product->pivot->cantidad, ['class' => 'form-control']) !!}
             {!! Form::hidden('id', $product->pivot->product_id, ['class' => 'form-control']) !!}

Please or to participate in this conversation.