Php Laravel-仅包含外键的表的模型(透视表)
我需要只使用两个外键实现表的模型。在我的数据库中,我有如下表:Php Laravel-仅包含外键的表的模型(透视表),php,mysql,laravel-4,Php,Mysql,Laravel 4,我需要只使用两个外键实现表的模型。在我的数据库中,我有如下表: product (id_product, ...) category_to_product (FK id_category, FK id_product) category (id_category, ...) 如何在Laravel中管理此连接?我是否应该实现合并表的模型以及它的外观类别到产品表不表示实体(/model),仅具有设计关系属性 数据库迁移 Schema::create('products', function(B
product (id_product, ...)
category_to_product (FK id_category, FK id_product)
category (id_category, ...)
如何在Laravel中管理此连接?我是否应该实现合并表的模型以及它的外观<代码>类别到产品表不表示实体(/model),仅具有设计关系属性
数据库迁移
Schema::create('products', function(Blueprint $table)
{
$table->increments('id_product');
// ...
});
Schema::create('categories', function(Blueprint $table)
{
$table->increments('id_category');
// ...
});
类别产品
Schema::create('category_to_product', function(Blueprint $table)
{
$table->integer('id_category')->unsigned();
$table->foreign('id_category')
->references('id_category')
->on('categories')
->onDelete('cascade');
$table->integer('id_product')->unsigned();
$table->foreign('id_product')
->references('id_product')
->on('products')
->onDelete('cascade');
});
产品
Schema::create('products', function(Blueprint $table)
{
$table->increments('id_product');
// ...
});
Schema::create('categories', function(Blueprint $table)
{
$table->increments('id_category');
// ...
});
类别
Schema::create('products', function(Blueprint $table)
{
$table->increments('id_product');
// ...
});
Schema::create('categories', function(Blueprint $table)
{
$table->increments('id_category');
// ...
});
请执行以下操作:
在型号类别中:
public function products(){
return $this->belongsToMany('Category');
}
public function categories(){
return $this->belongsToMany('Category', 'category_to_product');
}
public function categories() {
return $this->belongsTo('Category');
}
public function products() {
return $this->belongsTo('Product');
}
在型号产品中:
public function products(){
return $this->belongsToMany('Category');
}
public function categories(){
return $this->belongsToMany('Category', 'category_to_product');
}
public function categories() {
return $this->belongsTo('Category');
}
public function products() {
return $this->belongsTo('Product');
}
在型号类别产品中:
public function products(){
return $this->belongsToMany('Category');
}
public function categories(){
return $this->belongsToMany('Category', 'category_to_product');
}
public function categories() {
return $this->belongsTo('Category');
}
public function products() {
return $this->belongsTo('Product');
}
注意这些方法的命名! 这些名称与数据库表名称相同。见
回答。@pc shooter在创建方法方面是正确的 但您仍然必须首先在迁移过程中创建透视表
Schema::create('products', function(Blueprint $table)
{
$table->increments('id')
$table->string('name');
}
Schema::create('categories', function(Blueprint $table)
{
$table->increments('id')
$table->string('name');
}
然后是透视表
Schema::create('category_product', function(Blueprint $table)
{
$table->integer('category_id')
$table->foreign('category_id')->references('id')->on('categories');
$table->integer('product_id');
$table->foreign('product_id')->references('id')->on('products');
// And finally, the indexes (Better perfs when fetching data on that pivot table)
$table->index(['category_id', 'product_id'])->unique(); // This index has to be unique
}
可能重复的Hi,我已经创建了透视表和其他。但我也添加了
未签名
,因为在laravel和mysql中自动增量
使字段未签名,而级联删除
选项,因为没有产品或类别行是没有意义的。还为问题添加了代码<代码>$table->integer('id_category')->unsigned()$表->外部('id\u category')->引用('id\u category')->on('categories')->onDelete('cascade')你好,谢谢你的回复。在您的示例中,您使用了belongstomy
,但在另一个示例中,人们将此方法与with pivot
方法一起使用。需要时withPivot
method?当您希望从透视表手动检索信息时,需要withPivot。(例如,您可能有一个包含2个外键的透视表,还有第三列名为“retweet”。如果不调用withPivot方法,您将无法访问“retweet”列。为此,只需执行以下操作:Category::where('id',1)->withPivot()->first()->retweet@ChainList你应该把这个加到你的答案里。这是一个很好的解释!