technofreaks's avatar

Select products from multiple models to order

Hello

Have situation, where products are in two/three different models due to differences in product and data, model example blow.

How to add products from two product models to orders, not sure if polymorphic relation can be used ? Please help with example codes (I can follow examples better).

Is this approach is correct? My understanding of polymorphic relation is limited, I was only able to follow the example of comment and image. Cannot model this based on those example, Please provide example code for model and controller.

toys
    id - integer
    name - string
    gender_id - integer
    category_id - integer
    model - string 
 
books
    id - integer
    title - string
    genre_id - integer
    author_id - integer
    ISBN - string  
 
cloths
    id - integer
    title - string
    category_id - integer
    gender_id - integer    
    size_id - integer

orders
    id - integer
    name - text
    productable_id - integer
    productable_type - string

Thank You

0 likes
9 replies
jlrdw's avatar

I use ajax with lookup tables (some prefer dropdowns) to put them on the invoice line.

The number of models shouldn't matter.

technofreaks's avatar

@jlrdw My guess it will be in order placing page, Is there any example available ? I can follow example and have better understanding.

MohamedTammam's avatar

not sure if polymorphic relation can be used ?

Yes it can.

technofreaks's avatar

@MohamedTammam Example given is for comments and posts, which is kind of reverse, when comment is posted it saves in polymorphic relation, I was not able to understand, co-relate. An Example will be helpful.

Tray2's avatar

Why are you splitting the different types of products into models, that is really bad.

Just create a basic products table, then you create a product_properties table, where each product can have multiple different kinds of properties.

Read the One-to-many With Different Properties section of this post https://tray2.se/posts/database-design-part-2

1 like
technofreaks's avatar

Hey!! Tray2 :) I used your article to plan database tables , Very useful, Thank You

I did not take that approach for products because I don't know how to design single form for storing products showing only related fields when product type is selected. so I created to different models with own blade forms.

@Tray2 I was thinking about this also.. Will re-look into this, thank you

1 like
martinbean's avatar

How to add products from two product models to orders, not sure if polymorphic relation can be used

@technofreaks Well polymorphic relations are for exactly this scenario: where you want to use one of many possible options as a relation.

That being said, when creating orders, you want to copy product information over any way instead of relying on relations. If a user buys a book for $10 and you just add a relation to that book in the user’s order, if you later update the price of the book to $8, the user’s order total is also going to change, and then they‘re going to be angry why they’ve been charged $10 for what they see being a $8 order.

So, when placing an order, store the relation, but also copy over the product’s name and price at the time of purchase for historical reasons:

Schema::create('order_line_items', function (Blueprint $table): void {
    $table->id();
    $table->foreignId('order_id');
    $table->nullableMorphs('orderable');
    $table->string('description'); // Name of product
    $table->char('currency', 3); // 3-letter currency code, i.e. USD
    $table->unsignedInteger('unit_amount'); // Unit price of product
    $table->unsignedInteger('quantity');
});

You will now always know the name of the product when the customer bought it, as well as the price it was at the time of purchase.

1 like
technofreaks's avatar

@martinbean Useful point, Thank you

I am able to understand schema, have written belongsTo and hasMany relation in models, and controller.

But I did not understand Model and Controller example for polymorphic, as examples given in document is simple, its for image and comments in blog. I am looking for example of polymorphic , similar to belongsTo and hasMany relation.

1 like
martinbean's avatar

@technofreaks You just associate a model like you would a say, belongs to relation:

$item = new OrderLineItem();
$item->orderable()->associate($book);
$item->save();

Please or to participate in this conversation.