Database Explanation:
- Item has many tiers
- Tier has one type (Size, Option OR Addon)
- type has many-to-many relationship with option
- optionType has many sizes and price
The Problem: In eloquent query I am able to fetch data until option_type, I don't know how to create relationship of option_type pivot model with size model.
Eloquent Query:
$items = Item::with(['tiers' => function ($query) {
$query->with(['type' => function($query) {
$query->with('sizes')
->with('options')
->with('addons');
}]);
}])->where('id', 1)->get();
return $items;
What I want to Achieve: I want to eager load option_type with sizes, for every optiontype row i may have size and its price.
The Problem: when i write a query like:
$query->with(['options' => function($query) {
$query->with('sizes);
}])
It is basically saying that options have relationship with sizes which is not right, it should be optionType have relationship with size. how to can i get the data so that i would show me for record for each optiontype_id, size_id and price.
Type Model:
class Type extends Model
{
protected $table = 'types';
public function options()
{
return $this->belongsToMany(Option::class);
}
}
Option Model:
class Option extends Model
{
public function type()
{
return $this->belongsToMany(Type::class);
}
}
OptionType Model:
use Illuminate\Database\Eloquent\Relations\Pivot;
class OptionType extends Pivot
{
protected $table = 'option_type';
public function optiontypesize()
{
return $this->belongsToMany(Size::class, 'option_type_size', 'size_id', 'option_type_id');
}
}
Size Model:
class Size extends Model
{
public function optiontypesize()
{
return $this->belongsToMany(OptionType::class, 'option_type_size', 'size_id', 'option_type_id');
}
}