D
D
dima_maketov2017-04-14 15:49:33
MySQL
dima_maketov, 2017-04-14 15:49:33

Laravel. How to build relationships in a database?

Good afternoon!
Help deal with the issue.
There is a user's personal account (hereinafter referred to as LKP), it contains goods. I implemented this: the product belongs to the paintwork.
There are product formats, 3 types: for men, for women and for children. I have created a table in db with these formats.
1 column - id;
2nd column - format name.
Now you need to create a table (I created it, but maybe you can tell me what I did wrong) with the goods that the user ordered.
It looks like this:
1) id LKP.
2) item id.
3) format id.
Everything seems to be correct, but now I can not understand how to build relationships with this table.
Now the task is as follows: goods with a certain format are bought in the personal account and a basket with the selected goods should be displayed on the side. But so far I can’t figure out how to get through this jungle of relationships and get the necessary data, for example, display the format name next to the product name and product photo. Please tell me how to implement.
Here's what I have now:
LCP

public function photos()
  {
    return $this->hasMany(CartPhoto::class);
  }
  public function order()
  {
    return $this->hasMany(Order::class);
  }

Product
public function cart()
  {
    return $this->belongsTo(PersonalCart::class);
  }
  public function order()
  {
    return $this->belongsTo(Order::class);
  }

Format
public function order()
  {
    return $this->belongsTo(Order::class);
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
entermix, 2017-04-14
@entermix

It looks like this:
1) id LKP.
2) item id.
3) format id.

Wrong, do something like this:
users:
id, name, ...
offers:
id, name ...
offer_types
id, name, ...
orders
id, user_id ...
order_items
order_id, offer_id, type_id..

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question