Vladislav
Vladislav

Reputation: 143

Right relationships in Laravel

hello, sorry for my english. I will try explain my problem.

For example, we have model Product.

Each product has some options:

product_1 : option_1 = 50, option_2 = 14, option_3 = 23

Whats is the right way?

First - create database, like

id | title | option_1 | option_2 | option_3

Second - create models and tables, like

ProductModel hasMany optionModel 
OptionModel belongsToMany ProductModel

tables in databases: Product, Option, Product_Option_Relationships

Third - create some collection in function and table Product_Option_Relationships like

$options = collect([
  ['id' => '1', 'name' =>'option1'],
  ['id' => '2', 'name' =>'option2'],  
]);

Table: id | product_id | option_id

Or maybe exist normal way, because first - its too big table, when you have 20 options, second - create new model only for information function, i dont now, its normal? Third - too difficult in view show options name.

Thank you, i hope you understand me.

Upvotes: 0

Views: 48

Answers (3)

Gadzhev
Gadzhev

Reputation: 442

You can use many-to-many relationship and can structure it like so:

Product Model

class Product extends Model {
    ...

    public function options() {
        return $this->belongsToMany('App\Product', 'product_options', 'product_id', 'option_id');
    }
}

Options Model

class Option extends Model {
    ...

    public function product() {
        return $this->belongsToMany('App\Option', 'product_options', 'option_id', 'product_id');
    }
}

You will need three tables for this to work:

Table products

id | name
-----------------
 1 | iBeacon
 2 | Intel Edison

Table options

id | name  
----------
 1 | Price 
 2 | Size

Table product_options 

id | option_id | product_id
---------------------------
 1 |     1     |     2
 2 |     2     |     2

You can choose if you want to store a record with the option value in the options table or in the pivot table. I'd place them in the pivot table to keep the options table smaller.

Now you'll be able to assign options to your products like so:

...
// Assing options to a product
$product->options()->sync([$optionID, $optionID]);

// Get product's options
$product->options;

// Get products having certain option
$option->products;
...

Upvotes: 0

Nour
Nour

Reputation: 1497

Generally use the one-to-many, many-to-many relationships And the benefit for that you can freely edit any record without modifying the whole column to apply that on your tables :
First we have products table which is going to require options foreach so we should have the table options which is going to combine the options in general then we add a new table assignOptionsToProducts which is include keys for both options & products in this case you're going to have many-to-many or one-to-many relationship as you like

Products Table 
id | Name 
1  | Product A
2  | Product B

Options Table
id | Name 
1  | Option A
2  | Option B 

AssignOptionsToProducts Table 
id | Product_id | Option_id
1  | 1          | 1
2  | 1          | 2
3  | 2          | 2

As you can see we assigned the same option many times And when we want to modify any option you can without modifying each record in other tables and of course you can use each table many times easily

Upvotes: 1

João Pinho
João Pinho

Reputation: 365

Use the second way. You won't have repeated options. For example:

products

id | name
---|------
1  | Car
2  | Bike

options

id   |  name
-----|------------
1    |  Transport

option_product

option_id   |  product_id
------------|-------------
1           |  1
1           |  2

Using the other ways, you would have the option Transport twice.

Upvotes: 0

Related Questions