Home > Software engineering >  How to add an array in view and pass it to controller
How to add an array in view and pass it to controller

Time:02-04

Hello i am wondering if its possible to have an array in view blade that is filled with values that user selects and then to be passed to controller. I am asking if its possible to do such a thing and avoid this type of code i already have that works:

 foreach ($request->products as $index => $product) {

                $values[] = [
                    'order_id' => $order->id,
                    'product_id' => $product,
                    'amount' => $request->amount[$index],
                ];

So for the foreach i dont need to write $index => $product

This is the request that comes from view:

 $request->validate([
            'order_number' => 'required',
            'client_id' => 'required|exists:clients,id',
            'description' => 'required',
            'products' => 'required|exists:products,id',
            'amount' => 'required',
        ]);

And this is the view im using:

<div >
                                <label for="products" >{{ __('Product') }}</label>

                                <div >
                                    <select name="products[]" id="products" type="text"  required autocomplete="products">

                                        @foreach($products as $product)

                                            <option value="{{$product->id}}">{{$product->name}}</option>
                                        @endforeach

                                    </select>

                                    @error('products')
                                    <span  role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                    @enderror
                                </div>
                            </div>

                            <div >
                                <label for="amount" >{{ __('Amount') }}</label>

                                <div >
                                    <input id="amount" type="text"  name="amount[]" required autocomplete="amount">

                                    @error('amount')

                                    <span  role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>

                                    @enderror
                                </div>
                            </div>

CodePudding user response:

If I understand your question correctly, you want to avoid JQuery? It depends on what environment and language you're using. In ASP .NET Core you can use a hidden input field and pass something through as text.

<input hidden type="text" name="productsString" value="@JsonSerializer.Serialize(products)" />

Then in your controller you can deserialize it (C# example).

var products = JsonSerializer.Deserialize<Object>(productsString);

Maybe that's what you're looking for but it really depends on what you're doing, which I'm not sure about.

CodePudding user response:

We can use the input name to create an associative array using the product id as the key in the array.

You can achieve this by subbing in the product id for the array index and labelling the fields that will go into it.

<input type="number" name="products[ {{$product_id}} ][amount]">
<input type="text" name="products[ {{$product_id}} ][otherField]">

This will product a structure like

["products"]=> array(2) 
    { [101]=> array(2) { 
          ["amount"]=> string(2) "10" ["otherField"]=> string(7) "LABEL 1" }
      [102]=> array(2) { 
          ["amount"]=> string(2) "20" ["otherField"]=> string(7) "LABEL 2" }
    }

While you will still have to iterate using foreach($request->products as $productID => $data) the data structure is all relational regarding where the data is stored.

  • Related