1

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??

1
  • show me your form fields or tell me which models do you have Commented Nov 9, 2016 at 20:05

1 Answer 1

1

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'],
    ]);
}
Sign up to request clarification or add additional context in comments.

4 Comments

For your kind information My db structure is same as you describe here. Thank for your help @yohanan-baruchel
how to view my update form, with group of form remove button , like as my form picture (i.sstatic.net/Uxwxi.png)
Sorry buy i cant undersand your questiion about the remove button.. what are you trying to do?
i am new in laravel and stackoverflow too, but try to develop a eCommerce as a demo laravel project @yohanan-baruchelse

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.