Answer the question
In order to leave comments, you need to log in
How to simplify the handling of getting shared data?
In one category table.
In the second elements.
In the third color for the elements.
All of them are connected.
The point is to show in the category all the colors that its elements use.
Now I'm getting like this.
$categories = Category::with('elements.colors')->get();
@foreach($category->elements as $element)
{{$element->colors->code}}
@endforeach
Answer the question
In order to leave comments, you need to log in
You approach the task a little wrong - you need to display the unique color values used in the category elements, which means you need to create a relation in the category model that will get this data through the colors linking table. In Yii, it might look something like this:
class Category extends \yii\db\ActiveRecord{
// .....
public function getElements(){
return $this->hasMany(Element::className(), ['category_id' => 'id']);
}
public function getColors(){
return $this->hasMany(Color::className(), ['id' => 'element_id'])
->via('elements')->distinct();
}
// ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question