我正在使用数据透视表。我有一个模型类别和产品模型。
<?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');
}
}
当我使用时:
$products = Product::with('categories')->get();
转储查询,我得到以下查询:
"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将使用product_id和category_id完成。是否可以将这些设置为internal_id,或者我是否需要创建自定义查询?