我有3點型號的對象,即:Categories
,Category_products
& Products
多屬於關聯雄辯
分類:
<?php
class Categories extends Model
{
protected $table = 'categories';
public function product_ids() {
return $this->hasMany("app\Models\categories\Category_products", "category_id", "id");
}
?>
Category_products:
<?php
class Category_products extends Model
{
protected $table = 'category_products';
public function product_ids(){
return $this->belongsTo('app\Models\categories\Category');
}
public function products(){
return $this->hasOne("app\Models\Product", "id", "product_id");
}
}
?>
和產品:
<?php
class Product extends Model {
protected $table = 'products';
public function product(){
return $this->belongsTo("app\Models\categories\Category_products");
}
?>
現在在我的CategoryController我做的:
$this->data["products"] = Categories::find($id)->product_ids()->products()->get();
但現在我得到一個錯誤:
Call to undefined method Illuminate\Database\Query\Builder::products()
我怎樣才能做到這一點的正確方法?
你product_ids()返回一個屬於關聯關係「」應用程序\型號\ categories \ Category「,這可能是你錯誤的誘因。 – ChainList
它怎麼會導致錯誤,以及如何解決它? – Chilion