我一直在为Laravel 5.8应用程序设置一个Orders系统,但我想我已经(或者没有)使Orders系统的DB体系结构过于复杂。
我创建了以下表格:order
,order_items
,products
,services
,prices
,当然使用users
表作为“客户”。
我现在的疑问是在将products
和services
作为单独的表还是在items
的新表之间使用,这将说明该项目是“产品”还是“服务”
这是因为在建立Order::class
和OrderItem::class
模型的模型关系时,我似乎陷入了“打结”。
我的表格如下:
订单
+-------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| guid | varchar(191) | NO | | NULL | |
| user_id | int(10) unsigned | YES | MUL | NULL | |
| firm_id | bigint(20) unsigned | YES | MUL | NULL | |
| currency_id | int(10) unsigned | YES | MUL | 1 | |
| total | double(8,2) | NO | | 0.00 | |
| status | varchar(191) | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+-------------+---------------------+------+-----+---------+----------------+
order_items
+---------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| product_id | int(10) unsigned | YES | MUL | NULL | |
| service_id | bigint(20) unsigned | YES | MUL | NULL | |
| order_id | bigint(20) unsigned | NO | MUL | NULL | |
| quantity | double(8,2) | NO | | 1.00 | |
| quantity_type | varchar(191) | YES | | NULL | |
| notes | varchar(191) | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+---------------+---------------------+------+-----+---------+----------------+
产品
+--------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| guid | varchar(191) | NO | UNI | NULL | |
| name | varchar(191) | NO | | NULL | |
| name_slug | varchar(191) | NO | | NULL | |
| details | text | NO | | NULL | |
| has_shipment | tinyint(1) | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+--------------+---------------------+------+-----+---------+----------------+
服务
+-------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| guid | varchar(191) | NO | UNI | NULL | |
| name | varchar(191) | NO | | NULL | |
| name_slug | varchar(191) | NO | | NULL | |
| description | text | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+-------------+---------------------+------+-----+---------+----------------+
价格
+-------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| product_id | int(11) | YES | MUL | NULL | |
| service_id | bigint(20) unsigned | YES | MUL | NULL | |
| currency_id | int(10) unsigned | YES | MUL | NULL | |
| value | double(8,2) | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+-------------+---------------------+------+-----+---------+----------------+
用户(简体)
+-------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| guid | varchar(191) | NO | UNI | NULL | |
| name | varchar(191) | NO | | NULL | |
| name_slug | varchar(191) | NO | | NULL | |
| email | varchar(191) | NO | | NULL | |
| ... | ... | ... | ... | ... | ... |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+-------------+---------------------+------+-----+---------+----------------+
我的目标是使订单具有一个或多个产品和/或服务,并为用户提供各自的价格。< / p>
示例:
$order = Order::where('id', 1)->with('users', 'items', 'prices')->get();
但是我可以获取“订单”,“用户”,“物品”,而不是每个物品的“价格”。
我尝试过的事情
Order::class
和OrderItem::class
模型(例如:$this->belongsTo(Product::class)
)上的关系不同,但是似乎做得不好。
如上所述,我几乎能得到所有东西,但价格却不能。
问题:我应该将产品和服务合并到一个表中,并为类型添加一列(例如 service , product )?
在此先感谢您的任何建议。
答案 0 :(得分:1)
我认为合并这些表不是最好的方法。我认为,这是多态关系的完美用法。根据Laravel的文档:
多态关系允许目标模型使用单个关联属于一种以上类型的模型。
来源:https://laravel.com/docs/5.8/eloquent-relationships#many-to-many-polymorphic-relations
查看PHP embed html image as a link。如果您有任何疑问,请在这里评论。我很乐意为您提供帮助。