Answer the question
In order to leave comments, you need to log in
How to attach a model in laravel?
This model structure:
-Category
-Param
-ParamsToCategory
-ParamsValue
In the Category model I specify:
public function params()
{
return $this->hasManyThrough('Param', 'ParamsToCategory', 'category_id', 'id');
}
public function values()
{
return $this->hasMany('ParamsValue');
}
Category1: [
id
name
params : [
id
name
values : [
id,
name,
...
]
]
]
Category::with('params')->find($id);
Category::with('params')->with('values')->find($id)->toArray();
Answer the question
In order to leave comments, you need to log in
Попробуйте типа так:
Если внешние ключи правильно установлены, то подтягивает все values и params данной категории
Category::with('params')->find($id);
The construction is needed only if you do not want to make a request while accessing the params model parameter. Then the result will immediately lie there. In general, the stuy does not change much.
$Cat = Category::with('params')->find($id);
foreach(Cat->params as $param){
foreach($param->values as $value){
echo $value->{свойство};
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question