Laravel雄辩和多次连接 [英] Laravel Eloquent and Multiple Joins
问题描述
我知道如何使用Eloquent进行基本的查询和关系,但是在根据多个表中的关系选择信息时,我开始感到困惑。
例如,我可以使用查询构建器从数据库获取我需要的数据,如下所示:
$ data ['products'] = DB: :table('product')
- > select('product.id','product.ref_num','productdetails.name')
- > join('productdetails',function加入)
{
$ join-> on('product.id','=','productdetails.product_id')
- > where('productdetails.website_id' =','7');
})
- > leftJoin('product_category',function($ join)use($ submenu_id){
$ join-> on(' product.id','=','product_category.product_id')
- > where('product_category.category_id','=',$ submenu_id);
})
- > ; leftJoin('PR oduct_type',function($ join)use($ type_id){
$ join-> on('product.id','=','product_type.product_id')
- > where( 'product_type.type_id','=',$ type_id);
})
- > get();
基本上,我从产品和产品详细信息表中获取数据,根据产品所属的类别是什么类型的产品?这些是通过内部连接来定义的,用于将数据表product_type和product_category。\\ / p>
现在假设我有雄辩的关系正确设置,我如何在口才中这样做? / p>
以下是雄辩模特儿的相关部分
产品
class产品扩展Eloquent {
public function productdetails()
{
return $ this-> hasMany('Productdetail );
public function categories()
{
return $ this-> belongsToMany('Category','product_category','product_id','category_id');
}
public function types()
{
return $ this-> belongsToMany('Producttype','product_type','product_id','type_id' );
}
}
产品详情
class Productdetail extends Eloquent
{
public function product()
{
return $ this-> belongsTo('Product');
}
}
ProductType
class ProductTypes extends Enloquent {
function products()
{
return $ this-> belongsToMany('products','product_id','type_id','product_id');
}
类别
class Category Eloquent {
public function products()
{
return $ this-> belongsToMany('product','product_category ','category_id','product_id');
}
}
提前感谢
假设你的关系是正确的,相关的表名是:类别和类型,这将完成这个工作:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $查询 - > where('categories.id','=',$ submenu_id);
})
- > whereHas('types',function($ query)use($ type_id)
$ query-> where('types.id',$ type_id); //旁注:运算符'='是默认值,所以可以被忽略
})
- >得到();
它将运行2个查询(首先获得适当的产品,第二个是与他们相关的产品详细信息)回报雄辩收藏
I understand how to use Eloquent for basic queries and relationships, but I start getting confused when selecting information based on relationships in multiple tables.
For example, i can get the data I need from the database using the the query builder as follows:
$data['products'] = DB::table('product')
->select('product.id', 'product.ref_num', 'productdetails.name')
->join('productdetails', function($join)
{
$join->on('product.id', '=', 'productdetails.product_id')
->where('productdetails.website_id', '=', '7');
})
->leftJoin('product_category', function($join) use($submenu_id){
$join->on('product.id', '=', 'product_category.product_id')
->where('product_category.category_id', '=', $submenu_id);
})
->leftJoin('product_type', function($join) use($type_id){
$join->on('product.id', '=', 'product_type.product_id')
->where('product_type.type_id', '=', $type_id);
})
->get();
Basically, i'm getting data from the product and productdetails tables based on which category the product is part of and what type of product it is; These are defined by inner joins to pivot tables product_type and product_category.
Now assuming i have the eloquent relationships set up correctly, how would i go about doing this in Eloquent?
Here are the relevant parts of the Eloquent Models
Product
class Product extends Eloquent{
public function productdetails()
{
return $this->hasMany('Productdetail');
public function categories()
{
return $this->belongsToMany('Category', 'product_category', 'product_id', 'category_id');
}
public function types()
{
return $this->belongsToMany('Producttype', 'product_type', 'product_id', 'type_id');
}
}
Product Details
class Productdetail extends Eloquent
{
public function product()
{
return $this->belongsTo('Product');
}
}
ProductType
class ProductTypes extends Eloquent{
function products()
{
return $this->belongsToMany('products', 'product_id', 'type_id', 'product_id');
}
Category
class Category extends Eloquent{
public function products()
{
return $this->belongsToMany('product', 'product_category', 'category_id', 'product_id');
}
}
Thanks in advance
Assuming your relations are correct and related table names are: categories and types, this will do the job:
Product::with('productdetails')
->whereHas('categories', function ($query) use ($submenu_id) {
$query->where('categories.id', '=', $submenu_id);
})
->whereHas('types', function ($query) use ($type_id) {
$query->where('types.id', $type_id); // side note: operator '=' is default, so can be ommited
})
->get();
It will run 2 queries ( first for getting appropriate products, second for product details related to them) and return Eloquent Collection
这篇关于Laravel雄辩和多次连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!