<small id='TnLzk'></small><noframes id='TnLzk'>

    <i id='TnLzk'><tr id='TnLzk'><dt id='TnLzk'><q id='TnLzk'><span id='TnLzk'><b id='TnLzk'><form id='TnLzk'><ins id='TnLzk'></ins><ul id='TnLzk'></ul><sub id='TnLzk'></sub></form><legend id='TnLzk'></legend><bdo id='TnLzk'><pre id='TnLzk'><center id='TnLzk'></center></pre></bdo></b><th id='TnLzk'></th></span></q></dt></tr></i><div id='TnLzk'><tfoot id='TnLzk'></tfoot><dl id='TnLzk'><fieldset id='TnLzk'></fieldset></dl></div>
      <bdo id='TnLzk'></bdo><ul id='TnLzk'></ul>
    <legend id='TnLzk'><style id='TnLzk'><dir id='TnLzk'><q id='TnLzk'></q></dir></style></legend>

    1. <tfoot id='TnLzk'></tfoot>

      Laravel Eloquent 和多重连接

      时间:2023-09-24
      <i id='3KIZZ'><tr id='3KIZZ'><dt id='3KIZZ'><q id='3KIZZ'><span id='3KIZZ'><b id='3KIZZ'><form id='3KIZZ'><ins id='3KIZZ'></ins><ul id='3KIZZ'></ul><sub id='3KIZZ'></sub></form><legend id='3KIZZ'></legend><bdo id='3KIZZ'><pre id='3KIZZ'><center id='3KIZZ'></center></pre></bdo></b><th id='3KIZZ'></th></span></q></dt></tr></i><div id='3KIZZ'><tfoot id='3KIZZ'></tfoot><dl id='3KIZZ'><fieldset id='3KIZZ'></fieldset></dl></div>

        <tbody id='3KIZZ'></tbody>
      <tfoot id='3KIZZ'></tfoot>

      <small id='3KIZZ'></small><noframes id='3KIZZ'>

        <bdo id='3KIZZ'></bdo><ul id='3KIZZ'></ul>
          <legend id='3KIZZ'><style id='3KIZZ'><dir id='3KIZZ'><q id='3KIZZ'></q></dir></style></legend>

                本文介绍了Laravel Eloquent 和多重连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我了解如何将 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模板网!

                上一篇:如何在 Laravel 4 中手动创建一个新的空 Eloquent C 下一篇:获取 Eloquent 模型的关系数组

                相关文章

                最新文章

                <tfoot id='nLdM1'></tfoot>
                  • <bdo id='nLdM1'></bdo><ul id='nLdM1'></ul>
                1. <small id='nLdM1'></small><noframes id='nLdM1'>

                    <i id='nLdM1'><tr id='nLdM1'><dt id='nLdM1'><q id='nLdM1'><span id='nLdM1'><b id='nLdM1'><form id='nLdM1'><ins id='nLdM1'></ins><ul id='nLdM1'></ul><sub id='nLdM1'></sub></form><legend id='nLdM1'></legend><bdo id='nLdM1'><pre id='nLdM1'><center id='nLdM1'></center></pre></bdo></b><th id='nLdM1'></th></span></q></dt></tr></i><div id='nLdM1'><tfoot id='nLdM1'></tfoot><dl id='nLdM1'><fieldset id='nLdM1'></fieldset></dl></div>
                    <legend id='nLdM1'><style id='nLdM1'><dir id='nLdM1'><q id='nLdM1'></q></dir></style></legend>