Al-Amin
Al-Amin

Reputation: 798

How to store data from multiple dynamic form use laravel

My Dynamic form use blade template

Controller Code:

 $productPrice=$request->only('unit_id','size_id','color_id','price');
        dd($productPrice);

Controller dd output :

    array:4 [▼
  "unit_id" => array:3 [▼
    0 => "3"
    1 => "7"
    2 => "1"
  ]
  "size_id" => array:3 [▼
    0 => "2"
    1 => "1"
    2 => "4"
  ]
  "color_id" => array:3 [▼
    0 => "1"
    1 => "6"
    2 => "9"
  ]
  "price" => array:3 [▼
    0 => "32000"
    1 => "4280"
    2 => "5655"
  ]
]

How to store the data in product_price table use laravel easy way??

Upvotes: 1

Views: 2360

Answers (1)

user6612690
user6612690

Reputation:

In your form your'e using a repeater, this way you can't store the data in the same table of the product unless you convert the form data to string (serialize()) and store it in a 'price' row at the product table, this very bad way of storing data in DB.

The best practice for this kind of situations is making a new table for storing the different prices of the product, so your tables should look like this.

Products table:

|  id  |  name     | more columns | created_at         | updated_at        |
----------------------------------------------------------------------------
|   1  |"product1" |      0       |2016-01-31 00:27:16 |2016-01-31 00:27:16|
|   2  |"product2" |      1       |2016-01-31 00:27:16 |2016-01-31 00:37:16|
|   3  |"product3" |      0       |2016-01-31 00:27:16 |2016-01-31 01:47:04|

Products Prices Table:

|  id  |  product_id | unit | size | color | price |created_at| updated_at |
----------------------------------------------------------------------------
|   1  |      1      |  0   |  3   |   2   |   421 |2016-01.. |2016-01..   |
|   2  |      1      |  3   |  2   |   5   |   121 |2016-01.. |2016-01..   |
|   3  |      1      |  4   |  4   |   1   |   531 |2016-01.. |2016-01..   |

So this way, a product can have as many prices as you want, then the only thing that you have to do is to make relations in the models of those tables.

In the product model you can add:

public function prices() {
    return $this->hasMany(ProductPrice::class, 'product_id');
}

And of course that you will have to make the ProductPrice Model to make this relation work.

Personally I would also add a reation in the child model (the price model), like this:

public function product() {
    return  $this->belongsTo(Product::class, 'product_id');
}

UPDATE:

Now that you have those models, you can create a new ProductPrice item using the following code:

foreach($productPrice['unit_id'] as $k => $unit) {
    $product->prices()->create([
        'unit' => $productPrice['unit_id'][$k]
        'size' => (isset($productPrice['size_id'][$k])) $productPrice['size_id'][$k] ? : 0;
        'color' => (isset($productPrice['color_id'][$k])) $productPrice['color_id'][$k] ? : 0;
        'price' => (isset($productPrice['price'][$k])) $productPrice['price'][$k] ? : 0;
     ]);
}

But as you can see, this foreach seems a funny, this is because you're sending the form data of each price type as a array, so for make the code cleaner you can send the form array like this:

productPrice[][unit_id]
productPrice[][size_id]
productPrice[][color_id]
productPrice[][price]

So then your foreach code will look like this:

$productPrices = $request->only('productPrice');

foreach($productPrices as $productPrice) {
    $product->prices()->create([
        'unit'   => $productPrice['unit_id'],
        'size'   => $productPrice['size_id'],
        'color'  => $productPrice['color_id'],
        'price'  => $productPrice['price'],
    ]);
}

Upvotes: 1

Related Questions