extjac's avatar

Naming fields correctly so I can read and store them in DB

I am adding a very small ecommerce capability to an app. The plan is to have the ability to create Products and dynamically add options and attributes (Example below).

The issue I am having is that I am not able to name the fields in a way that I can read them with a foreach() or for().

Example of Data

	return [
		'name' => 'Hockey Jersey',
		'prices' => [
			'base_price' =>'200',
			'sales_price' =>'180',
		],
		'options' => [
			[
				'name' => 'Size',
				'attributes' => [ 
					[ 'label'=> 'Small'	  ,'price' => 0 ],
					[ 'label'=> 'Meddium' , 'price' => 0 ], 
					[ 'label'=> 'Large'   ,  'price' => 10 ],
					[ 'label'=> 'XLarge'  , 'price' => 15 ]
				]
		 	],[
 				'name' => 'Color',
				'description' => 'some description',
				'attributes' => [ 
					[ 'label'=> 'Green'	  ,'price' => null ],
					[ 'label'=> 'Blue' , 'price' => null ], 
					[ 'label'=> 'Red'   ,  'price' => 25],
				]
			]
	 	],
	];

Basically, i am not able to name the fields properly

#product name
<input type="text" name="name" value="" placeholder="Product Name">

#option 1
<input type="text" name="option['name']" value="" placeholder="color">
<input  type="text"  name="attr[0]['name']" placeholder="Green or Blue" />
<input  type="text"  name="attr[0]['price']" placeholder="10" />

#option 2
<input type="text" name="option['name']" value="" placeholder="size">
<input  type="text"  name="attr[1]['name']" placeholder="Small" />
<input  type="text"  name="attr[1]['price']" placeholder="15" />

usually I would read the fields like below, but in this case are nested.

foreach( request()->fields as $i => $val ){}
0 likes
2 replies
extjac's avatar
extjac
OP
Best Answer
Level 6

In case that someone needs to use something like this in the future....Or if I forget how I did it...

 Product           
<input name="product_name"   value="Jersey" /> 
<input name="product_price"   value="200" /> 

Options#1 
<input name="options[][name]" placeholder="Color or Size" value="Size" /> 
Attr#1
<input type="text"  name="options[0][attributes][0][title]" value="Small" /> 
<input type="text"  name="options[0][attributes][0][price]" value="35"/>
<input type="text"  name="options[0][attributes][0][quantity]" value="23"/>
Attr#2
<input type="text"  name="options[0][attributes][1][title]" value="Large" /> 
<input type="text"  name="options[0][attributes][1][price]" value="20"/> 
<input type="text"  name="options[0][attributes][1][quantity]" value="74"/> 
Attr#3
<input type="text"  name="options[0][attributes][2][title]" value="XLarge" /> 
<input type="text"  name="options[0][attributes][2][price]" value="45"/> 
<input type="text"  name="options[0][attributes][2][quantity]" value="4"/> 

Options#2
<input name="options[][name]" placeholder="Color or Size"  value="Color" /> 
Attr#1
<input type="text"  name="options[1][attributes][0][title]" value="Black" /> 
<input type="text"  name="options[1][attributes][0][price]" value="25"/>
<input type="text"  name="options[1][attributes][0][quantity]" value="22"/>
 Attr#2
<input type="text"  name="options[1][attributes][1][title]" value="Green" /> 
<input type="text"  name="options[1][attributes][1][price]" value="70"/> 
<input type="text"  name="options[1][attributes][1][quantity]" value="7"/> 

then a simple foreach()

        foreach( request()->options  as $option )
        {
            echo  'Option Name: '. $option['name'] .'<br>' ;

            foreach( $option['attributes'] as $attribute  )
            {
                echo 'Title: ' . $attribute['title'] . '<br>' ;
                echo 'Price: ' . $attribute['price'] . '<br>' ;
                echo 'Quantity: ' . $attribute['quantity'] . '<br><br>' ;
            }
        }
extjac's avatar

I am converting from array to json; but i am having a small problem. I would like to remove the array number

Array
(
    [1] => Array
        (
            [name] => Size
            [attributes] => Array
                (
                    [0] => Array
                        (
                            [title] => S
                            [price] => 1
                            [quantity] => 11
                        )

                    [1] => Array
                        (
                            [title] => M
                            [price] => 2
                            [quantity] => 22
                        )

                    [2] => Array
                        (
                            [title] => L
                            [price] => 3
                            [quantity] => 33
                        )

                )

        )

    [2] => Array
        (
            [name] => Colors
            [attributes] => Array
                (
                    [0] => Array
                        (
                            [title] => Blue
                            [price] => 1
                            [quantity] => 11
                        )

                    [3] => Array
                        (
                            [title] => Green
                            [price] => 2
                            [quantity] => 22
                        )

                )

        )

)

because json looks like this and it am having some issues

{
  "1": {
    "name": "Size",
    "attributes": [
      {
        "title": "S",
        "price": "1",
        "quantity": "11"
      },
      {
        "title": "M",
        "price": "2",
        "quantity": "22"
      },
      {
        "title": "L",
        "price": "3",
        "quantity": "33"
      }
    ]
  },
  "2": {
    "name": "Colors",
    "attributes": {
      "0": {
        "title": "Blue",
        "price": "1",
        "quantity": "11"
      },
      "3": {
        "title": "Green",
        "price": "2",
        "quantity": "22"
      }
    }
  }
}

Please or to participate in this conversation.