I have 3 models, Industry, Category, Machine and they are linked as following
Problem : I want to show all machines in an industry, how i can do that?
What I did so far : I retrieved all category ids related to a specific industry like : $industry->categories()->pluck("id"). I don't know what I can do next to get all machines
Industry
model relation:
class Industry {
public function categories(){
return $this->hasMany(Category::class);
}
}
Category
model relation:
class Category {
public function indusry(){
return $this->hasOne(Industry::class);
}
public function machines(){
return $this->belongsToMany(Machine::class);
}
}
Machine
model relation:
class Machine {
public function categories(){
return $this->belongsToMany(Category::class);
}
}
you can use whereHas
method like this:
$machines = Machine::whereHas('categories', function($query) {
$query->whereHas('indusry', function($sub_query) {
$sub_query->where('name', {your_industry});
});
});