ctomasz

This is old database structure for Prestashop - but I think it will be some kind of inspiration for you.

At graph you can see relations between models and what kind of data should belong to particular model.

https://www.prestashop.com/blog/en/a-new-physical-data-model-available-for-prestashop/

madsynn

@ctomasz @Snapey

Thanks for the info but I am looking for an example of how to submit the requested form data into sub tables in my product store method.

something along the lines of this:

 if(!empty($productPricing)){
             foreach ($productPricing as $productPrice)
             {
                 Price::create([
                     'title' => $productPrice->title,
                     'price' => $productPrice->price,
                     'model' => $productPrice->model,
                     'sku' => $productPrice->sku,
                     'upc' => $productPrice->upc,
                     'quantity' => $productPrice->quantity,
                     'alt_details' => $productPrice->alt_details
                 ]);
             }
         }

The data is submitted form the create product form and needs to be submitted into a seperate database with id corresponding to the product itself.

madsynn

@Snapey

i added my files to a temporary repo if you need more of the code then i have put on this thread.

https://github.com/madsynn/laravelEcom.git

i have everything working but the pricing and the additional fields on the album photos. any help is greatly welcome.

Snapey
Snapey
2 years ago (1,044,115 XP)

Unfortunately you have not shared the form, which is where the problem starts

First, If you are not keeping track of the price->id then when saving the product, you must first delete all existing prices since you will create all new records from form data.

Secondly, each price, with all its attributes, must come from the form with a unique key so that they can be separated out into individual writes. I suggested this as by adding the price->id to each row in the prices table, but it does not need to be the id, it can be a simple counter

<td><input type="text" name="price[$i]['sku']"></td>
<td><input type="text" name="price[$i]['upc']"></td>
<?php $i++>
</tr>

The point is that price has two levels of array indices price[level 1][level 2] level 1 groups all the attributes for one price, level2 is all the individual price attributes. Your earlier code just used the name price over and over so you were not creating the right form data structure.

You will then get back from the form, a simple multi-dimension array, each top level representing one row in your table. You can then iterate over these with your for each.

just another observation;

I generally name relationships that are hasMany as plurals so productPrice() on product, I would have called prices so that I can remember the relationship, and product where it is a belongsTo on the Price model

madsynn

@Snapey Thank you for the great info. I am going to try it now. Sorry i forgot the form. Here is the form for my pricing.

JS

$("#addRow").click(function (e) {
        var i = 0;  
    i++;
            console.log("Insert Data For New Product Price");
             e.preventDefault();
              $("#addRow").click(function() {
                   var row = $("<tr>" + "<td><input type='text' class='text-center form-control' name='price[i]['title']' /></td><td><input type='text' class='text-center form-control' name='price[i]['model']' /></td>" +
                   "<td><input type='text' class='text-center form-control  currency' name='price[i]['price']' placeholder='$0.00' data-affixes-stay='false' data-prefix='$ ' data-thousands=',' data-decimal='.' /></td>" +
                   "<td><input type='text' class='text-center form-control' name='price[i]['quantity']' /></td>" +
                   "<td><input type='text' class='text-center form-control' name='price[i]['sku']' /></td>" +
                   "<td><input type='text' class='text-center form-control' name='price[i]['upc']' value='636343' /></td>" +
                   "</tr>" + "<tr class='alt'><td colspan='1' class='text-center labelTextarea'><label ><strong>Product Variation Details:</strong></label></td>" +
                   "<td colspan='4'><textarea rows='3' class='form-control details-input' name='price[i]['alt_details']' placeholder='add the details or difference here:'></textarea></td>" +
                   "</tr>");
                   $("table#product-pricing-table tbody").append(row);
                   $('table#product-pricing-table input.currency').maskMoney();
               });
               $(function() {
                   $('table#product-pricing-table input.currency').maskMoney();
               });

HTML

 <table class="table table-striped table-hover table-bordered" id="product-pricing-table">
                        <thead>
                            <tr>
                                <th>Title:</th>
                                <th>Model:</th>
                                <th>Price:</th>
                                <th>Quantity:</th>
                                <th>SKU:</th>
                                <th>UPC:</th>
                            </tr>
                        </thead>
                        <tbody>
                            @if(isset($product) && $product->prices->count()<0)
                                @foreach($product->prices as $price)
                                    <tr class="alt">
                                        <td><input type="text" class="form-control" name="price[i]['title']" value="{!! $price->model !!}" /></td>
                                        <td><input type="text" class="text-center form-control" name="price[i]['model']" value="{!! $price->model !!}" /></td>
                                        <td><input type="text" class="text-center form-control currency" name="price[i]['price']" value="{!! $price->price !!}" data-affixes-stay="false" data-prefix="$ " data-thousands="," data-decimal="." /> </td>
                                        <td><input type="text" class="text-center form-control" name="price[i]['quantity']" maxlength="4" value="{!! $price->quantity !!}" /></td>
                                        <td><input type="text" class="text-center form-control" name="prices[sku]" value="{!! $price->sku !!}" /></td>
                                        <td><input type="text" class="text-center form-control" name="price[i]['upc']" maxlength="12" value="{!! $price->upc !!}" /></td>
                                   
                                    </tr>
                                    <tr class="alt">
                                        <td colspan="1" class="labelTextarea"><label>  <strong>Product Variation Details:</strong></label></td>
                                        <td colspan="5"> <textarea rows="3" class="form-control details-input" name="price[i]['alt_details']" value="{!! $price->alt_details !!}"></textarea></td>
                                    </tr>
                                    <tr class="spacer invis"><td colspan="5"></td></tr>
                                @endforeach
                            @else
                                    <tr class="alt">
                                        <td><input type="text" class="form-control" name="price[i]['title']" value="" /></td>
                                        <td><input type="text" class="text-center form-control" name="price[i]['model']" value="" /></td>
                                        <td><input type="text" class="text-center form-control currency" name="price[i]['price']" placeholder="$0.00" data-affixes-stay="false" data-prefix="$ " data-thousands="," data-decimal="." /></td>
                                        <td><input type="text" class="text-center form-control" name="price[i]['quantity']" maxlength="4" /></td>
                                        <td><input type="text" class="text-center form-control" name="price[i]['sku']" value="" /></td>
                                        <td><input type="text" class="text-center form-control" name="price[i]['upc']" maxlength="12" value="636343" /></td>
                               
                                    </tr>

                                    <tr class="alt">
                                        <td colspan="1" class="labelTextarea"><label><strong>Product Variation Details:</strong></label></td>
                                        <td colspan="5"><textarea rows="3" class="form-control details-input" name="price[$i]['alt_details']" placeholder="add the details or difference here:"></textarea></td>
                                    </tr>
                                    <tr class="spacer invis"><td colspan="5"></td></tr>
                            @endif
                        </tbody>
                    </table>
madsynn

@Snapey

I tried what you recommended but to no avail. can you please take a look at my files and see if you can find why i cannot get the prices to save to the database. I have worked on this for a week now and am really frustrated. Do you by chance freelance?

my files are added to this.

https://github.com/madsynn/laravelEcom.git

Thank you for taking the time to help me.

Snapey
Snapey
2 years ago (1,044,115 XP)

I have a copy of your app. Where is the issue (which controller / view)?

Snapey
Snapey
2 years ago (1,044,115 XP)

OK, this is what I meant earlier;

here you are looping over prices but giving them all the same identity, so there is no way to differentiate between the price fields when they are returned. Give each row a unique price key, and since every existing price has an id then why not use that?

    @foreach($product->prices as $price)
        <tr class="alt">
            <td><input type="text" class="form-control" name="prices[{{ $price->id }}][title]" value="{!! $price->model !!}" /></td>
            <td><input type="text" class="text-center form-control" name="prices[{{ $price->id }}][model]" value="{!! $price->model !!}" /></td>
            <td><input type="text" class="text-center form-control currency" name="prices[{{ $price->id }}][price]" value="{!! $price->price !!}" data-affixes-stay="false" data-prefix="$ " data-thousands="," data-decimal="." /> </td>
            <td><input type="text" class="text-center form-control" name="prices[{{ $price->id }}][quantity]" maxlength="4" value="{!! $price->quantity !!}" /></td>
            <td><input type="text" class="text-center form-control" name="prices[{{ $price->id }}][sku]" value="{!! $price->sku !!}" /></td>
            <td><input type="text" class="text-center form-control" name="prices[{{ $price->id }}][upc]" maxlength="12" value="{!! $price->upc !!}" /></td>
        </tr>
        <tr class="alt">
            <td colspan="1" class="labelTextarea"><label>  <strong>Product Variation Details:</strong></label></td>
            <td colspan="5"> <textarea rows="3" class="form-control details-input" name="prices[{{ $price->id }}][alt_details]" value="{!! $price->alt_details !!}"></textarea></td>
        </tr>
        <tr class="spacer invis"><td colspan="5"></td></tr>
    @endforeach
Snapey
Snapey
2 years ago (1,044,115 XP)

Saving;

        //needs more thought about handling new prices. This just copes with editing existing ones
        foreach ($request->prices as $productPrice)
        {
            $price =  Price::findOrFail($productPrice['id'])

            $price->title       = $productPrice['title'];
            $price->price       = $productPrice['price'];
            $price->model       = $productPrice['model'];
            $price->sku         = $productPrice['sku'];
            $price->upc         = $productPrice['upc'];
            $price->quantity    = $productPrice['quantity'];
            $price->alt_details = $productPrice['alt_details'];
            $price->save();
        }

        //or, assuming $fillable correct on model..
        foreach ($request->prices as $productPrice) {
            Price::where('id',$productPrice['id'])->update($productPrice);
        }
madsynn

@Snapey

Thank you for your help i will try them in the morning. I have another question about the saving also. When i does save it saves all new prices but ties them to the product id. which is what its supposed to do. but in my products view in the admin instead of the product listing only once it is listing the product for each new product. Do you know why its doing this. that would be the product.blade.php in the backend/ecom/products/ part of the views.

madsynn

@Snapey " //needs more thought about handling new prices. This just copes with editing existing ones"

Not sure what you mean here. wouldn't this be handled in the store method. this is the part im having troubles with. the $product = new Product::create(); is the part that will not work for me.

Snapey
Snapey
2 years ago (1,044,115 XP)

Not quite sure why this is so difficult to grasp?

If you have a bunch of prices on a form, then they all need a unique key so that they can be represented at the backend. In the examples I gave I used the existing Price id as the unique key and then overwrote the previous value in the database. This is an edit function after all.

//needs more thought about handling new prices. This just copes with editing existing ones"

what I mean here is that you have an edit form, where maybe you have 4 price variations listed, with SKU code, etc for each

then in your form, you have the option to add extra rows for more variants. Each of these new rows must be uniquely identified .

Your options.

  1. Use the example I have shown, but then create the new rows with a name like name="newPrice[i]['sku'] where i starts at 0 and increments by 1 each time you add a new row. Then in the save method, loop through the prices as I showed, and then do the same again, but this time foreach($request->newPrice as $np) or similar and Create new entries rather than updating existing ones as in my example.

  2. Don't use the existing price id, and just assign a counter to each row in the table, irrespective if its a new price or not. In the save method, first delete all the old product prices (bear in mind if there is a fault in saving you could lose all the rows), and then create all new prices.

instead of the product listing only once it is listing the product for each new product.

This does not make a lot of sense? Not sure what you mean?

madsynn

@Snapey Thank you for your response.

instead of the product listing only once it is listing the product for each new product.

What i mean here is in my admin when i got to products.blade.php where i get a list of all the products, when i create a new product with multiple prices it saves as the same product the same number of the alternate prices. Instead of one product.

question on:

Don't use the existing price id, and just assign a counter to each row in the table, irrespective if its a new price or not. In the save method, first delete all the old product prices (bear in mind if there is a fault in saving you could lose all the rows), and then create all new prices.

So you are telling me not to use a counter here because it might overwrite the products that are already listed in those rows. correct? how would i add new prices. The product->price has not been created yet so i cannot get the priceID what can i use for this part. i see you use prices[{{ $price->id }}][quantity] with $price->id but until i save the product the price does not have an id correct?

Snapey
Snapey
2 years ago (1,044,115 XP)

I thought this was an edit function, and you are passing a collection of prices?

In your code;

@foreach($product->prices as $price)

so you are looping through all the existing prices for the product? Each of these will have an id according to the entry in the prices table.

I think you need to be clear here. are you creating a new product from scratch or are you editing one that exists already?

madsynn

@Snapey Sorry about that i thought i clarified it already. I am creating new product with multiple prices. The update stuff you helped me with will probably work great if i can get them created.

Please sign in or create an account to participate in this conversation.