How to implement model selection by brand?

Imagine it this way:
Table
Goods - the products table contains the fields id, brand_id, model_id.

Brands table of brand name, contains fields id, name

Models - a table with names of models that contains the fields id, parent_id, name


You will need to display the goods in the form "Manufacturer: {{ brand_id (name) }} model {{ model_id (name) }}".
No idea how to relate the tables so that when you select manufacturer, you could only select the corresponding model.

Third day I can not deal with this issue, similar examples are not found. the experience isn't great, preferably by example.
June 3rd 19 at 18:58
1 answer
June 3rd 19 at 19:00
Solution
1) it is Not clear why you make the relationship many-to-many. Because one product can have only one producer and one producer of many goods. Emerges communications O:M. Is done classically in the table item column brand_id. In the model of the brand method
public function models()
{
 return $this->hasMany(Model::class);
}


A similar method and model models (tautology)
public function brands()
{
 return $this->it's belongsto(Brand::class);
}


To retrieve:
Model::findOrFail($id)->brand->name;
//Or so
Brand::findOrFail($id)->models()->where('id', $id)->first()->name;
//Or so in view
@foreach(Brand::findOrFail($id)->models as $model)
Manufacturer {{$brand->name}} : model {{$model->name}}
@endforeach


2) If you are directly critical to make a choice many-to-many, then the fetch is done using whereHas

Find more questions by tags Laravel