Laravel Elounten拥有许多细节-产品、类别、颜色
我需要选择某个类别中具有特定颜色的所有产品,这将在类别视图(使用表格指定的颜色)中包含该类别的所有产品,但仅包含所选颜色 我试过这个。。。但我知道我错了!我不太懂哈斯曼的故事 在Prodotto模型中:Laravel Elounten拥有许多细节-产品、类别、颜色,laravel,eloquent,has-many-through,Laravel,Eloquent,Has Many Through,我需要选择某个类别中具有特定颜色的所有产品,这将在类别视图(使用表格指定的颜色)中包含该类别的所有产品,但仅包含所选颜色 我试过这个。。。但我知道我错了!我不太懂哈斯曼的故事 在Prodotto模型中: public function categoria() // il nome del metodo è il metodo della relazione { return $this->hasOne(Categoria::class, '
public function categoria() // il nome del metodo è il metodo della relazione
{
return $this->hasOne(Categoria::class,
'id','categoria_id'
);
}
public function colori()
{
return $this->belongsToMany(Colore::class , 'prodotto_colore')->withTimestamps();
}
在Colore模型中:
public function prodottiCategoriaColore()
{
return $this->hasManyThrough(Prodotto::class, Categoria::class);
}
在模型中:
public function prodotti() // il nome del metodo è il metodo della relazione
{
return $this->hasMany(Prodotto::class,
'categoria_id','id'
);
}
在分类控制器中:
private function colore($categoriaId , $coloreId)
{
$categoria_qb = Categoria::query()
->where('id', '=', $categoriaId)->with('prodotti');
$prodottiColore_qb = $categoria_qb->where('colore_id' , $coloreId)->with('prodottiCategoriaColore');
$prodotti = $prodottiColore_qb->first();
return view('frontend.categoria', ['prodotti' => $prodotti]);
}
在DB中,我有一个prodotti表、一个colori表、一个category表和一个pivot prodotto_colore表(产品ID-颜色ID)所以一个类别有很多产品,一个产品有很多颜色,是吗?是的。。。对的所以一个类别有很多产品,一个产品有很多颜色,是吗?是的。。。对的