問題描述
我了解如何將 Eloquent 用于基本查詢和關系,但在根據多個表中的關系選擇信息時,我開始感到困惑.
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();
基本上,我根據產品所屬的類別和產品類型從產品和產品詳細信息表中獲取數據;這些是由數據透視表 product_type 和 product_category 的內連接定義的.
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.
現在假設我正確設置了 eloquent 關系,我將如何在 Eloquent 中執行此操作?
Now assuming i have the eloquent relationships set up correctly, how would i go about doing this in Eloquent?
這里是 Eloquent 模型的相關部分
Here are the relevant parts of the Eloquent Models
產品
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');
}
}
產品詳情
class Productdetail extends Eloquent
{
public function product()
{
return $this->belongsTo('Product');
}
}
產品類型
class ProductTypes extends Eloquent{
function products()
{
return $this->belongsToMany('products', 'product_id', 'type_id', 'product_id');
}
類別
class Category extends Eloquent{
public function products()
{
return $this->belongsToMany('product', 'product_category', 'category_id', 'product_id');
}
}
提前致謝
推薦答案
假設你的關系是正確的并且相關的表名稱是:categories 和 types,這將完成這項工作:
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();
它將運行 2 個查詢(第一個用于獲取合適的產品,第二個用于與它們相關的產品詳細信息)并返回 Eloquent Collection
It will run 2 queries ( first for getting appropriate products, second for product details related to them) and return Eloquent Collection
這篇關于Laravel Eloquent 和多重連接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!