我需要获得必须与存储在数据透视表中的值匹配的模型,但不幸的是我无法获得解决方案。
这是我的架构
PEROPERTY TABLE
id
FILTER TABLE
id
FILTER_OPTION TABLE
id
filterId
FILTER_OPTION_TRANSLATE TABLE
optionId
languageId
title
PROPERTY_FILTER TABLE
propertyId
filterId
optionId
我想做的是:
@foreach($property->filters as $filter)
{{ $filter->option->translate->title }}
@endforeach
但这里对我来说问题是如何说获取选项匹配 PROPERTY_FILTER TABLE 中的 optionId
我的模型:
PROPERTY MODEL
public function filters()
{
return $this->belongsToMany(Filter::class, 'PROPERTY_FILTER','propertyId','filterId');
}
FILTER MODEL
public function option()
{
return $this->hasMany(Filter_Option::class, 'filterId');
}
FILTER OPTION MODEL
public function translate()
{
return $this
->hasOne(Filter_Option_Translate::class, 'optionId')
->where('langId', currentLanguage()->langId);
}
我希望我能得到一些帮助,从现在开始谢谢。
我通过使用数据透视表作为分离模型解决了我的问题。
我试图通过数据透视表获得 3 级远关系,但即使是中间模型也无法解决我的问题,我只是尝试了一个单独的模型。
首先,我创建了表示 property_filter 数据透视表的 Property_Filter 模型,并添加了过滤器和选项方法,如下所示:
public function filter()
{
return $this->belongsTo(Filter::class, 'filterId');
}
public function option()
{
return $this->belongsTo(Filter_Option::class, 'filterValue');
}
然后转换过滤器关系方法
public function filters()
{
return $this->belongsToMany(Filter::class, 'PROPERTY_FILTER','propertyId','filterId');
}
至
public function filters()
{
return $this->hasMany(Property_Filter::class,'propertyId');
}
现在我到达为迭代过滤器选择的过滤器和选项,如下所示:
@foreach($properties as $property)
@foreach($property->filters as $filter) // here i get filters chosen for iterated property
<span class="caption">{{ $filter->filter->translate->entryTitle }}</span> // here i get iterated filters title (translated)
<span class="value">{{ $filter->option->translate->entryTitle }}</span> // here i get chosen option for iterated filter not all options belognsto iterated filter
@endforeach
@endforeach
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句