php Laravel Eloquent 和多重连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22842977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:18:31  来源:igfitidea点击:

Laravel Eloquent and Multiple Joins

phplaravel-4eloquent

提问by Rob

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.

我了解如何将 Eloquent 用于基本查询和关系,但在根据多个表中的关系选择信息时,我开始感到困惑。

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.

基本上,我根据产品所属的类别和产品的类型从产品和产品详细信息表中获取数据;这些是由数据透视表 product_type 和 product_category 的内连接定义的。

Now assuming i have the eloquent relationships set up correctly, how would i go about doing this in Eloquent?

现在假设我正确设置了 eloquent 关系,我将如何在 Eloquent 中执行此操作?

Here are the relevant parts of the Eloquent Models

以下是 Eloquent 模型的相关部分

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

提前致谢

回答by Jarek Tkaczyk

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

它将运行 2 个查询(第一个用于获取合适的产品,第二个用于与它们相关的产品详细信息)并返回 Eloquent Collection

回答by Hashmat Waziri

//routes.php

//路由.php

 /* relationship is : content has images(one to many). And I am extracting the images based on the name that is given in the search input ($keyword = Input::get('keyword') */


   $dbresults = DB::table('contents')->join('images', 'images.content_id', '=', 'contents.id')->where('contents.Name', 'LIKE', '%' .$keyword. '%')->get();

   return View::make('/results')->with("results", $dbresults);

View

看法

@foreach($results as $result)
{{ $result->image_1 }}
@endforeach