Database product-Larvel关系的第二个属性selectlist
我在这个问题上工作了一段时间,但似乎没有找到理想的解决方案 我在一家有产品的网店工作。这些产品可以包含不同的尺寸、颜色或/和材料 我想要什么:Database product-Larvel关系的第二个属性selectlist,database,laravel,relationships,selectlist,Database,Laravel,Relationships,Selectlist,我在这个问题上工作了一段时间,但似乎没有找到理想的解决方案 我在一家有产品的网店工作。这些产品可以包含不同的尺寸、颜色或/和材料 我想要什么: 具有多个选择列表的产品。例如:如果产品是“裤子”,客户应该能够从selectlist大小和其他selectlist颜色中进行选择 我拥有的 DB: Table Products: Id|Name|Price|etc... Table: Properties: Id|Name|Value Table Product_properties: Id|P
具有多个选择列表的产品。例如:如果产品是“裤子”,客户应该能够从selectlist大小和其他selectlist颜色中进行选择 我拥有的
DB:
Table Products: Id|Name|Price|etc...
Table: Properties: Id|Name|Value
Table Product_properties: Id|Product_id|Property_1_id|Property_2_id|Supply
为了能够使用Laravel正确地访问它们,我还创建了这些扩展到eloquent的类。
我将这些类中的关系设置为:
产品:
public function productProperties()
{
return $this->hasMany('ProductProperty', 'product_id');
}
ProductProperty:
public function product()
{
return $this->belongsTo('Product');
}
public function properties()
{
return $this->hasMany('Property', 'id');
}
财产:
public function properties()
{
return $this->belomgsTo('ProductProperty');
}
我的问题这个配置正确吗?如何有效地将它们放入selectlist 我稍微修改了模式,并提出了一个不错的解决方案。这看起来很混乱,但实际上很简单 每种类型的属性都应该有自己的表,在本例中是大小和材质。这将使添加其他类型的属性变得非常容易,因为您只需创建表和模型,就可以立即开始使用它,而无需对代码进行其他更改
class Size extends Eloquent {
public function properties()
{
return $this->morphMany('Property', 'imageable');
}
}
class Material extends Eloquent {
public function properties()
{
return $this->morphMany('Property', 'imageable');
}
}
为了将所有属性组合在一起并使它们更易于使用,我创建了一个名为属性
的多态表,该表将属性本身的id存储在其表中及其所属的类、大小
或材质
class CreatePropertiesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('properties', function($table)
{
$table->increments('id');
$table->integer('imageable_id');
$table->string('imageable_type');
});
}
}
此表将用于两个目的,它显然将充当管理我们与尺寸和材料模型的多态关系的表,并且它将是与products表的多对多关系的一部分(一个产品可以具有多个属性,而一个属性可以属于多个产品)。这是那张桌子的模型
class Property extends Eloquent {
public function imageable()
{
return $this->morphTo();
}
public function products()
{
return $this->belongsToMany('Product');
}
}
最后,为了完成我们的产品/属性关系,我们将有products
表
class CreateProductTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function($table)
{
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
}
而产品
模型
class Product extends Eloquent {
public function properties()
{
return $this->belongsToMany('Property');
}
}
最后,产品/属性多对多关系表
class CreatePropertyProductTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_property', function($table)
{
$table->increments('id');
$table->integer('product_id');
$table->integer('property_id');
});
}
}
考虑到这一点,回答你最初的问题变得非常容易。要获得尺寸,只需使用Size
model
$Size=Size::all()->列出('id','name')代码>
要将它们放入选择列表,在创建视图时,请在控制器或路由中传递变量
返回视图::make('some.View')->带有('size',$size)代码>
在我看来
{{Form::select('size',$size,Input::old('size'))}
要获得某个产品的所有属性,这也相当容易
$product = Product::find(1);
foreach($product->properties as $property) {
$property = $property->imageable;
$class = get_class($property);
echo $class;
echo ': ';
echo $property->name;
echo "<br />";
}
$product=product::find(1);
foreach($product->属性为$property){
$property=$property->imageable;
$class=get_class($property);
echo$类;
回声':';
echo$property->name;
回声“
”;
}
您可能最终会希望根据所选产品限制可用尺寸(或其他属性、颜色/材质等),在这种情况下,我建议添加一个额外的表来管理这些尺寸,每个可能的产品/属性组合都有一行。例如,一个product\u size
表,其中每个可能的产品/尺寸组合都有一行。然后在填写选择值时查询该关系$size=Product::find(1)->possibleSizes代码>您是否有可能稍微修改一下您的模式,或者您是否仍然坚持现有模式?有一些巨大的改进可以让你的生活变得更加轻松。如果有必要,我可以马上改变它。我仍在设计中,所以为了我的理解;为了存储剩余的供应数量,我必须创建另一个表(除了您提到的表之外),该表存储“product_id”、多个“property_id”和“supply_int”?因为真正重要的是,例如,一个产品的尺寸:S(黑色、白色)、M(蓝黑色)、L(黄色、橙色)、XL(白色、黑色、黄色、橙色、蓝色)。用另一种方式说:如何存储尺寸和颜色组合的可用性?在建议的表结构中,该表是什么样子的??