4

I'm trying to create a shopping cart and I want to POST multiples fields with same name and process them

<?php foreach($datacart as $datacart) { ?>
<form method="post" action="/update-cart">

<input type="hidden" name="rowid" value="{{$datacart->rowid}}">
<input type="text" name="quantity" value="{{$datacart->quantity}}">

 <?php }>

<input type="submit" value="update">

</form>


public function update_cart(request $request){

   $rowId = $request->rowid;
   $quantity = $request->quantity;



   Cart::update($rowId, $quantity);

}

First displaying all products using a foreach. Then each item has a hidden rowid and a quantity.

Below code is showing my controller. But with this code, it only updates one item (lastone). But I want to update each product's quantity separately

Forgot to mention, I use https://github.com/Crinsane/LaravelShoppingcart as my cart plugin

4 Answers 4

6

First of lets improve your blade file, you can use blade syntax instead of PHP, so here:

<form method="post" action="/update-cart">
    @csrf

    @foreach($datacart as $datacart)
        <input type="hidden" name="rowid[]" value="{{$datacart->rowid}}">
        <input type="text" name="quantity[]" value="{{$datacart->quantity}}">
    @endforeach

    <input type="submit" value="update">
</form>

so notice the name attributes of the input elements now are accepting multiple values as an array. Then to process it in your controller you can do the following:

public function update_cart(Request $request) {

    foreach ( $request->rowid as $index => $id ) {
       Cart::find($id)->update(['quantity' => $request->quantity[$index]]);
    }

}

-- EDIT

Just tested calling update on the model as static method won't work.

Sign up to request clarification or add additional context in comments.

1 Comment

this will only works, if the fields id and quantity are marked as fillable in the Cart model - laravel.com/docs/5.8/eloquent#mass-assignment
1

You need to append [] after the name

<input type="hidden" name="rowid[]" value="{{$datacart->rowid}}">
<input type="text" name="quantity[]" value="{{$datacart->quantity}}">

Then in your controller method

public function update_cart(Request $request) {
  for ($i = 0; $i < count($request->input('rowid')); $i++) {
    $cart = Cart::find($request->input('rowid')[$i];

    $cart->quantity = $request->input('quantity')[$i];
  }
}

Comments

0

So, what you need to do is define in the html markup is that the input is an array like this:

i.e. using square brackets [] next to the name of the input elements

<?php foreach($datacart as $datacart) { ?>
<form method="post" action="/update-cart">

<input type="hidden" name="rowid[]" value="{{$datacart->rowid}}">
<input type="text" name="quantity[]" value="{{$datacart->quantity}}">

 <?php }>

<input type="submit" value="update">

</form>

and then when you handle the request in the controller method, you need to process it like this:

public function update_cart(request $request) {
    foreach ($request->rowid as $index => $rowid) {
        $quantity = $request->quantity[$index];
        Cart::where('id', $rowId)
            ->update([
                'quantity' => $quantity
            ]);
    }
}

Comments

0

thanks all. this code worked for me. i used for loop with a index

<input type="hidden" name="rowid[]" value="{{$datacart->rowid}}">
<input type="text" name="quantity[]" value="{{$datacart->quantity}}">

,

 for ($i = 0; $i < count($request->input('rowid')); $i++){

            Cart::update($request->rowid[$i], $request->quantity[$i]);

        }

1 Comment

You should accept an answer that helped you, as all the suggestions were what you just showed. And not answering with what helped you taken from another answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.