的專欄中,我有三個表:雄辯指相關的模型
categories
id, title
products
id, name
categories_products
id, category_id, product_id
我還設置了根據模型和關係(包括有其他的belongsToMany)
現在我想獲得屬於一個類別
Category::where('title','Electronics')->first()->products()->limit(10)->get(['products.name']);
的正常工作的所有產品,但我也希望包括每個產品的類別標題,以及:
Category::where('title','Electronics')->first()->products()->limit(10)->get(['products.name','category.title']);
但是它返回:列未找到category.title
我認爲關係會照顧它。
編輯:型號 - >
類別:
class Category extends Model
{
protected $fillable = array('title');
public function products()
{
return $this->belongsToMany('Product', 'categories_products', 'category_id', 'product_id');
}
}
class Product extends Model
{
protected $fillable = array('name');
public function categories()
{
return $this->belongsToMany('Category', 'categories_products', 'product_id', 'category_id');
}
}
您可以張貼在每個模型的關係:
然後訪問該產品? –
看起來你在'categories'上引用'name',但name是在'products'上? –
@EricTucker添加並修復了 – Chris