查询多个选项属性
Query multiple options attributes
我已经创建了一个搜索表单,但我需要一些关于如何查询数据和结果的帮助:
基本上,我有一个产品列表,每个产品都有一个product_type
,而且每个产品可以有很多材料属性。
我已经进行了查询以获取产品类型,这很容易,因为 product type id
在同一个 table(产品)中,但我必须使用用户选择的材料(属性)。
我的控制器:
public function searchResults(Request $request)
{
if($request->has('type')){
$type = $request->type;
}
if($request->has('material')){
$material = $request->material;
}
$query = \DB::table('products');
//only one type
if ($request->has('type') && $type) {
$query->where('product_type_id', $type);
}
// multiple values
if ($request->has('material') && $material) {
//create query to get all the products with the materials selected
// the materials request comes in a array of ids ([1,2,3])..
}
$products = $query->get();
return view('catalogs.index',compact('products'));
}
}
产品型号:
class Product extends Model
{
public function photos()
{
return $this->hasMany(ProductImage::class, 'product_id','id');
}
public function businessAreaName()
{
return $this->hasOne(ProductBusinessarea::class,'id','product_businessarea_id');
}
public function typeName()
{
return $this->hasOne(ProductType::class,'id','product_type_id');
}
public function businessAttributes()
{
return $this->hasMany(ProductAttributeBusinessarea::class);
}
public function materialAttributes()
{
return $this->hasMany(ProductAttributeMaterial::class);
}
public function areas(){
return $this->belongsToMany(ProductAttributeBusinessarea::class);
}
public function materials(){
return $this->belongsToMany(ProductAttributeMaterial::class);
}
}
数据库:
产品
- id
- 姓名
- type_id
product_attribute_materials
- product_id
- product_material_id
product_materials
- id
- 姓名
我如何结合查询来获得所有产品与所选材料?
您可以加入 tables,
$query = DB::table('products')
->join("product_attribute_materials","products.id","=","product_attribute_materials.product_id")
->whereIn("product_attribute_materials.product_material_id", $material)
->get();
或
您可以从 "product_attribute_materials" (table) 获取第一个产品 ID,然后从产品 table 获取产品。
喜欢,
$material = DB :: table("product_attribute_materials")
->pluck('product_id')
->whereIn("product_material_id", $material)->toArray();
$query->whereIn('id', $material);
希望对您有所帮助。
我已经创建了一个搜索表单,但我需要一些关于如何查询数据和结果的帮助:
基本上,我有一个产品列表,每个产品都有一个product_type
,而且每个产品可以有很多材料属性。
我已经进行了查询以获取产品类型,这很容易,因为 product type id
在同一个 table(产品)中,但我必须使用用户选择的材料(属性)。
我的控制器:
public function searchResults(Request $request)
{
if($request->has('type')){
$type = $request->type;
}
if($request->has('material')){
$material = $request->material;
}
$query = \DB::table('products');
//only one type
if ($request->has('type') && $type) {
$query->where('product_type_id', $type);
}
// multiple values
if ($request->has('material') && $material) {
//create query to get all the products with the materials selected
// the materials request comes in a array of ids ([1,2,3])..
}
$products = $query->get();
return view('catalogs.index',compact('products'));
}
}
产品型号:
class Product extends Model
{
public function photos()
{
return $this->hasMany(ProductImage::class, 'product_id','id');
}
public function businessAreaName()
{
return $this->hasOne(ProductBusinessarea::class,'id','product_businessarea_id');
}
public function typeName()
{
return $this->hasOne(ProductType::class,'id','product_type_id');
}
public function businessAttributes()
{
return $this->hasMany(ProductAttributeBusinessarea::class);
}
public function materialAttributes()
{
return $this->hasMany(ProductAttributeMaterial::class);
}
public function areas(){
return $this->belongsToMany(ProductAttributeBusinessarea::class);
}
public function materials(){
return $this->belongsToMany(ProductAttributeMaterial::class);
}
}
数据库:
产品
- id
- 姓名
- type_id
product_attribute_materials
- product_id
- product_material_id
product_materials
- id
- 姓名
我如何结合查询来获得所有产品与所选材料?
您可以加入 tables,
$query = DB::table('products')
->join("product_attribute_materials","products.id","=","product_attribute_materials.product_id")
->whereIn("product_attribute_materials.product_material_id", $material)
->get();
或
您可以从 "product_attribute_materials" (table) 获取第一个产品 ID,然后从产品 table 获取产品。 喜欢,
$material = DB :: table("product_attribute_materials")
->pluck('product_id')
->whereIn("product_material_id", $material)->toArray();
$query->whereIn('id', $material);
希望对您有所帮助。