Laravel雄辩的嵌套查询
我和拉威尔一起工作,陷入了困境。我有以下型号:Laravel雄辩的嵌套查询,laravel,eloquent,relationship,Laravel,Eloquent,Relationship,我和拉威尔一起工作,陷入了困境。我有以下型号: 类别 产品 类别产品 CategoryProduct保存关于哪个产品属于哪个类别(一个产品可能属于多个类别)的信息 现在,当我想加载属于某个特定类别的所有产品时,我需要对Product和CategoryProduct运行查询,这正是我遇到的问题 我进行了以下尝试,但没有成功: $products = Product::where('status', '=', 'active') ->where('category_id', '=', $c
- 类别
- 产品
- 类别产品
CategoryProduct
保存关于哪个产品属于哪个类别(一个产品可能属于多个类别)的信息
现在,当我想加载属于某个特定类别的所有产品时,我需要对Product
和CategoryProduct
运行查询,这正是我遇到的问题
我进行了以下尝试,但没有成功:
$products = Product::where('status', '=', 'active')
->where('category_id', '=', $category_id)
->take($count)
->skip($skip)
->get();
显然,它会说category\u id
不是一列
以下是我的数据库和模型结构:
类别表
class Category extends Eloquent {
protected $table = 'categories';
public static $rules = array('name' => 'required|min:3');
}
身份证,
名称
等等
产品表
class Category extends Eloquent {
protected $table = 'categories';
public static $rules = array('name' => 'required|min:3');
}
身份证,
名称
sku,
等等
类别产品表
class Category extends Eloquent {
protected $table = 'categories';
public static $rules = array('name' => 'required|min:3');
}
身份证,
产品标识(product.id的外键)
category_id,(category.id的外键)
等等
产品型号
class Category extends Eloquent {
protected $table = 'categories';
public static $rules = array('name' => 'required|min:3');
}
类别模型
class Category extends Eloquent {
protected $table = 'categories';
public static $rules = array('name' => 'required|min:3');
}
类别产品型号
class Category extends Eloquent {
protected $table = 'categories';
public static $rules = array('name' => 'required|min:3');
}
<?php
class CategoryProduct extends Eloquent {
protected $table = 'category_products';
public function product()
{
return $this->belongsTo('Product');
}
public function category()
{
return $this->belongsTo('Category');
}
}
表类别产品将产品id、类别id作为列
class Category extends Eloquent {
protected $table = 'categories';
public static $rules = array('name' => 'required|min:3');
}
现在,查询:
$products=Product::where('status','=','active')->take($count)->skip($skip)->get()
将仅从产品表中选择产品。如果我检查每个产品是否存在于category_products中,那么对于大量的产品,将有太多的数据库查询
class Category extends Eloquent {
protected $table = 'categories';
public static $rules = array('name' => 'required|min:3');
}
任何想法,如何实现这一点。我希望我能摆脱困境。感谢除非除了产品id和类别id之外还有指向其他关系的其他字段,否则不需要使用
类别产品
模型
必要的是在类别
和产品
模型上建立关系的方法
在类别中
,添加关系函数
公共功能产品()
{
返回$this->belongtomany('Product','category_products');
}
在您的产品
型号中,对类别执行相同的操作
public function categories()
{
返回$this->belongtomany('Category','Category_products');
}
然后,您可以使用关系方法和whereHas()
$products=Product::whereHas('categories',function($q)use($category\u id)
{
$q->where('id',$category\u id);
})->其中('状态','活动')
->拿($count)
->跳过($skip)
->get();
在多对多关系中,数据透视表不需要模型。请参阅雄辩的文档以获得进一步的解释
您仍然需要创建迁移来设置数据透视表(如果不使用迁移,则手动创建),但不需要创建模型。相反,为Category
创建一个函数来指定关系:
public function products()
{
return $this->belongsToMany('App\Product', 'category_products');
// - You might need to adjust the namespace of App\Product
// - category_products refers to the pivot table name
}
同样,产品
也需要类似的公共功能
然后,您可以通过另一种方式来完成,找到该类别,然后列出其所有相关产品:
$products = Category::find($category_id)
->products()
->where('status', 'active')
->take($count)
->skip($skip)
->get();
也可能与您的相关。查看
的has
/where has
功能。嘿,用户3158900谢谢您的快速回复。使用CategoryProduct是因为产品可能属于多个类别。@Ashutosh多对多透视表不需要模型。看一下我的回答中的文档链接,明白你想说的了,再次感谢。现在查看文档。当您在父表上使用belongtomany
时,Laravel将为您处理和管理透视表(category\u prodcuts
)。您需要做的唯一一件事就是告诉Laravel该表的名称,我已在回答中编辑/添加了该表。@user1669496您好,但如果该产品属于供应商,该怎么办?而不是$product=Products::whereHas()
,而是$vendor=Vendors::whereHas()
。供应商是产品的母公司。如何在多个嵌套关系下使用该条件?