例如,我有一个产品,我有一个基本产品.
在产品模型中,我指定了以下内容:
//In class Product
public function BaseProduct()
{
return $this->belongsTo("BaseProduct", "BaseProductId");
}
In the BaseProduct, I've specified the following relationship:
//In class BaseProduct
public function Products()
{
return $this->hasMany("Product", "ProductId");
}
If I were to select a product, like so:
$Product::first()
我可以通过以下操作获得基本产品:
$Product::first()->BaseProduct()->get();
Instead of getting the array of the result from that, how would I get the Model
of the BaseProduct, so I can get all of the children of BaseProduct, meaning all Products that have a foreign key relating to this BaseProduct.
我try 了BaseProduct()->all();
次,但这不是一个有效的方法.
Edit:
I've created the following chain of function calls - but it's awful.
return BaseProduct::find(Product::first()->BaseProduct()->getResults()['BaseProductId'])->Products()->getResults();
最终编辑:
我在我的BaseProduct
型车上犯了一个错误.在Products()
函数中,我指定了return $this->hasMany("Product", "ProductId");
,其中ProductId
应该是BaseProductId
.
在我修复它之后,我可以成功地使用:
Product::first()->BaseProduct->products;
正如谢赫·赫拉所解释的那样.