0

how to get hasmany relation inside another relation in Laravel

i want to get product optionGroup with product options

i made this

    $try1 = Product::with(["optionGroups.options"])->find(1);

but this return all group options

i want to get product optionGroup with product options only

i want it to be like

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

models

class Product extends Model
{
    public function options()
    {
        return $this->hasMany(Option::class, 'product_id');
    }
    public function optionGroups()
    {
        return $this->belongsToMany(OptionGroup::class, 'options')->groupBy("id");
    }
}

class Option extends Model
{
    public function product()
    {
        return $this->belongsTo(\App\Models\Product::class, 'product_id', 'id');
    }
    public function optionGroup()
    {
        return $this->belongsTo(\App\Models\OptionGroup::class, 'option_group_id', 'id');
    }
}

class OptionGroup extends Model
{
    public $table = 'option_groups';
    public $fillable = [
        'name'
    ];
    public function options()
    {
        return $this->hasMany(Option::class, 'option_group_id');
    }
}

Schema::create('products', function (Blueprint $table) {
    $table->id('id');
    $table->string('title');
});
Schema::create('options', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name', 127);
    $table->double('price', 8, 2)->default(0);
    $table->integer('product_id')->unsigned();
    $table->integer('option_group_id')->unsigned();
});
Schema::create('option_groups', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name', 127);
});

please help me

4
  • Have you tried? Product::with(["options.optionGroup"])->find(1); Commented Apr 1, 2021 at 2:53
  • Yes but this will return the group inside the option Commented Apr 1, 2021 at 5:51
  • I want the option inside the group Commented Apr 1, 2021 at 5:51
  • Have you looked at the hasManyThrough relation? Commented Apr 1, 2021 at 14:25

2 Answers 2

2
Product::whereHas('optionGroups', function($query){
     $query->with(['options']);
})->first();
Sign up to request clarification or add additional context in comments.

3 Comments

This will return the group inside the option .... i want the option inside the group 👍👍
@yemenpoint I just updated the answer . Once try that and let me know
Hi Bipin this doesn't return any option or any optionGroup
0

Try adding this to your Product model

public function optionGroupsAndOptions()
{
    return $this->optionGroups->with('options')->where('product_id', $this->id);
}

then

$try1 = Product::with("optionGroupsAndOptions")->find(1);

3 Comments

Not work i even tried this. return $this->optionGroups()->with(["options" => function($q){ $q->where("product_id",$this->id); }]);
$this->id is null and this["id"] is null
Try this $try1 = Product::with("optionGroupsAndOptions")->where('product_id', 1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.