Php Laravel 4多个2多个更改记录.id到其他内容
我正在使用数据透视表。我有一个模型类别和一个产品模型Php Laravel 4多个2多个更改记录.id到其他内容,php,mysql,laravel-4,many-to-many,Php,Mysql,Laravel 4,Many To Many,我正在使用数据透视表。我有一个模型类别和一个产品模型 <?php class Product extends \Eloquent { protected $table = 'products'; public function categories(){ return $this->belongsToMany('Product', 'category_product', 'category_id', 'product_id'); } }
<?php
class Product extends \Eloquent {
protected $table = 'products';
public function categories(){
return $this->belongsToMany('Product', 'category_product', 'category_id', 'product_id');
}
}
<?php
class Category extends \Eloquent {
protected $table = 'categories';
public function products(){
return $this->belongsToMany('Category', 'category_product', 'product_id', 'category_id');
}
}
然后转储查询,我得到以下查询:
"select `categories`.*, `category_product`.`category_id` as `pivot_category_id`, `category_product`.`product_id` as `pivot_product_id` from `categories` inner join `category_product` on `categories`.`id` = `category_product`.`product_id` where `category_product`.`category_id` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
如您所见,innerJoin将使用产品id和类别id完成。是否可以将两者都设置为内部id,还是需要创建自定义查询?您所说的“将它们设置为内部id”是什么意思?你能描述一下你在寻找什么样的行为吗?我觉得这个查询有点奇怪,因为它从
category
表中获取,加入category\u product
透视表,然后过滤透视表中的category\u id
。但是该id当然已经存在于类别中,而没有连接。这真的是幼虫生成的查询吗?你说的“将它们设置为内部_id”是什么意思?你能描述一下你在寻找什么样的行为吗?我觉得这个查询有点奇怪,因为它从category
表中获取,加入category\u product
透视表,然后过滤透视表中的category\u id
。但是该id当然已经存在于类别中,而没有连接。这真的是幼虫生成的查询吗?
"select `categories`.*, `category_product`.`category_id` as `pivot_category_id`, `category_product`.`product_id` as `pivot_product_id` from `categories` inner join `category_product` on `categories`.`id` = `category_product`.`product_id` where `category_product`.`category_id` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"