0

I am working an multiple select Boxes in a form which are rendered dynamically.

Here in the below scenario I am mapping the selecttion to the parent title.

The example result is { "1": [ 2 ], "2": [ 1, 3 ] }

        <table class="table">
          <thead>
            <tr>
              <td>Variation Name</td>
              <td>Variation Values</td>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Size</td>
              <td>
                <select multiple="multiple">
                  <option value="2">Medium</option>
                </select>
              </td>
            </tr>
            <tr>
              <td>Color</td>
              <td>
                <select multiple="multiple">
                  <option value="1">White</option>
                  <option value="3">Blue</option>
                  <option value="4">Black</option>
                </select>
              </td>
            </tr>
          </tbody>
        </table>

I am passing the result to the Laravel Controller so that I could save the response..

I am not sure how do I save the data to the database..

public function itemsStore(Request $request)
    {
        $items_arrays = array($request['itemsArray'], true);
        dd(items_arrays);
    }

The dd result is

array:2 [
  0 => "{"1":[2],"2":[1,3]}"
  1 => true
]

How do I save the values to database in the respective format

item_id | item_value_id
   1             2
   2             1
   2             3

I am populating the above object using Vue. Sending the data to controller through axios library. Fiddle

1
  • How are you submitting the form i.e. are you using ajax or just the standard form submit? Commented Aug 29, 2020 at 11:48

3 Answers 3

1

In PHP you have different ways to define an array.

One way is to define it by setting its values explicitly, like:

<?php
  $my_array_1 = array("first" => 0, "second" => 1) // or;
  $my_array_2 = array(0, 1, 2);
?>

The arrays can store mixed values, so this is valid:

<?php
  $my_array_3 = array(0, "one", 2)
?>

Your array is like the one before:

<?php
  $items_arrays = array($request['itemsArray'], true);
?>

The PHP array() doesn't have a parameter only the items added, so the true you put at the end is just the second element of your array. https://www.php.net/manual/en/function.array.php

Based on that your dd outputs exactly what you added when you defined the array:

array:2 [
  0 => "{"1":[2],"2":[1,3]}" // this is the string from $request['itemsArray']
  1 => true // this is the second element you added
]

I think your problem is that you need to parse the string that you receive in your $request['itemsArray'].

  1. So, first:
public function itemsStore(Request $request)
  {
    $json = json_decode($request['itemsArray']);
    // $json is now an associative array in PHP
    // something like: array(1 => array(2), 2 => array(1, 3))
    
    // $items_arrays = array($request['itemsArray'], true);
    // dd(items_arrays);
  }
  1. Then you need to flatten this associative array:
public function itemsStore(Request $request)
  {
    $json = json_decode($request['itemsArray']);
    // $json is now an associative array in PHP
    // something like: array(1 => array(2), 2 => array(1, 3))
    $items_arrays = [];
    foreach(json as $key1 => $val1) {
      foreach($val1 as $key2 => $val2) {
        $items_array[] = array($key1 => $val2);
      }
    }
    // $items_arrays = array($request['itemsArray'], true);
    dd(items_arrays);
  }

(json_decode has optional parameters: https://www.php.net/manual/en/function.json-decode.php)

Of course, you should check the $request before working with it - authentication & validation are important!


Sorry, if the syntax is not correct - I haven't worked with PHP for over a year and wrote the samples by heart (no checking). But the idea is surely correct:

  1. Get the data from the request (Validate the data! This is not required for the process, but required for a production environment!)
  2. Transform it from the string format to PHP array
  3. Read it in a final array (as you need it in the end)
  4. Put the final format in the database
Sign up to request clarification or add additional context in comments.

Comments

0

you need to give some name to dynamic select. Fore example if you have a list [size, color] of attributes you could do

  • ...
  • ...

In the server side you will receive a array param attr[]

$attr = request()->attr;

it should looks like $attr['size'] => [ your size selected] $attr['color'] => your color selected.

You could make a dump to watch the form.

I hope it helps, please let me know.

1 Comment

Thank you for response. Here is the fiddle on how I a getting the data. jsfiddle.net/eurodgj7. Any Enhancement would be much appreciated
0

Try this alternate

Make sure in your html you set the name attribute to an array like below

<select multiple="multiple" name="sizes[]" id="sizes">

Then to store it you can get it in the controller like

$sizes = $request->input('sizes');//it will give you string separated by commas
$sizes = explode(',', $sizes);// to separate it explode the string

// save it to database

Similarly for other inputs like multi select colors input you can use the above steps

1 Comment

Thank youy for quick reponse. I am using Vue to populate the objects. I am storing the above object to itemsArray while passing to laravel controller. Here is the fiddle jsfiddle.net/eurodgj7

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.