如何在Laravel中得到另一个关系中的多个关系

如何在Laravel中得到另一个关系中的多个关系,laravel,eloquent,has-many,has-and-belongs-to-many,Laravel,Eloquent,Has Many,Has And Belongs To Many,如何在Laravel中得到另一个关系中的多个关系 我想获得产品选项带有产品选项的组 我做的 $try1 = Product::with(["optionGroups.options"])->find(1); 但这将返回所有组选项 我想获取仅包含产品选项的产品选项组 我希望它是这样的 { "title": "product name", "optionGroups": [ { "name": "Size", "option

如何在Laravel中得到另一个关系中的多个关系

我想获得产品选项带有产品选项的组

我做的

    $try1 = Product::with(["optionGroups.options"])->find(1);
但这将返回所有组选项

我想获取仅包含产品选项的产品选项组

我希望它是这样的

{ "title": "product name", "optionGroups": [ { "name": "Size", "options": [ { "name": "XL", "price": 1200 }, { "name": "L", "price": 1000 } ] } ] }
请帮助我

尝试将此添加到您的产品型号中

Product::whereHas('optionGroups', function($query){
     $query->with(['options']);
})->first();
public function optionGroupsAndOptions()
{
    return $this->optionGroups->with('options')->where('product_id', $this->id);
}
然后


你试过了吗<代码>产品::with([“options.optionGroup”])->查找(1)是,但这将返回选项内的组我希望选项在组内您查看了hasManyThrough关系吗?这将返回选项内的组。。。。我想要@yemenpoint组中的选项我刚刚更新了答案。一旦尝试,让我知道Hi Bipin这不会返回任何选项或任何选项组无效,我甚至尝试了这个。返回$this->optionGroups()->with([“options”=>函数($q){$q->where(“product_id”,$this->id);}])$此->id为空,而此[“id”]为空尝试此
$try1=Product::with(“optionGroupsAndOptions”)->其中('Product_id',1)
public function optionGroupsAndOptions()
{
    return $this->optionGroups->with('options')->where('product_id', $this->id);
}
$try1 = Product::with("optionGroupsAndOptions")->find(1);