【发布时间】:2016-04-01 01:44:31
【问题描述】:
我在 Laravel 5 上创建应用程序时遇到了问题。 我正在尝试创建产品的属性,这些属性取决于属性类型(整数、字符串、布尔值)。
有一个“产品”表:
- id - int、pk、ai、uns
- 名称 - 字符串
- 代码 - 字符串
- 价格 - 浮动 null
'属性'表:
- id - int、pk、ai、uns
- 名称 - 字符串
- 代码 - 字符串
产品型号代码:
class Product extends Model
{
public function properties()
{
return $this->belongsToMany(Property::class);
}
}
物业型号代码:
class Property extends Model
{
public function products()
{
return $this->belongsToMany(Product::class);
}
public function integers()
{
return $this->morphedByMany(PropertyInteger::class, 'property_type', 'product_property');
}
public function strings()
{
return $this->morphedByMany(PropertyString::class, 'property_type', 'product_property');
}
}
PropertyInteger 代码(ProductString 代码相同):
class PropertyInteger extends Model
{
public function properties()
{
return $this->morphToMany(Property::class, 'property_type', 'product_property');
}
}
然后我尝试获取产品的所有属性及其值: (new App\Product)->with(['properties', 'properties.integers', 'properties.strings']); 我得到的所有属性都不是属于 Product 模型的,但是在值中我得到了所有属性的所有值。
谢谢。
【问题讨论】: