Answer the question
In order to leave comments, you need to log in
How to implement the choice of model by manufacturer?
I imagine it like this:
Tables
Goods - goods table, contains id, brand_id, model_id fields.
Brands - table with brand names, contains id, name fields
Models - table with model names, contains id, parent_id, name fields
Answer the question
In order to leave comments, you need to log in
1) It is not clear why you made a many-to-many relationship. After all, one product can have only one producer, and one producer can have many products. An O:M relationship emerges . It is done classically - in the product table, the brand_id column. In the brand model, the method
public function models()
{
return $this->hasMany(Model::class);
}
public function brands()
{
return $this->belongsTo(Brand::class);
}
Model::findOrFail($id)->brand->name;
//Или так
Brand::findOrFail($id)->models()->where('id', $id)->first()->name;
//Или так во вьюшке
@foreach(Brand::findOrFail($id)->models as $model)
Производитель {{$brand->name}} : модель {{$model->name}}
@endforeach
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question