首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拉勒维尔雄辩的GroupBy在自己加入的时候不工作

拉勒维尔雄辩的GroupBy在自己加入的时候不工作
EN

Stack Overflow用户
提问于 2017-09-19 06:14:16
回答 2查看 574关注 0票数 0

我有一张桌子,上面写着工业,结构如下:

代码语言:javascript
复制
 id  |        industry        | status_id     | parent_industry_id 
-----+------------------------+---------------+---------------------+
 1   |     Parent Industry 1  |       1       |
 2   |     Child Industry 1   |       1       |        1
 3   |     Child Industry 2   |       1       |        1        
 4   |     Parent Industry 2  |       1       |
 5   |     Child Industry 3   |       1       |        4
 6   |     Child Industry 4   |       1       |        4   

现在,我需要在我的模型中将它显示为父->子对象输出。我可以加入并获得详细信息,但是GroupBy条款似乎不起作用。仍然得到平坦的层次结构输出

我的问题是:

代码语言:javascript
复制
 $result = DB::table('industry as t1')
                  ->join('industry AS t2', 't2.parent_id', '=', 't1.id')
                  ->groupBy('t1.id','t2.id')
                  ->select(
                           't1.id as parent_id',
                           't1.industry as parent_industry',
                           't2.id as child_id',
                           't2.industry as child_industry',
                           't2.parent_id'
                           )
                  ->where('t1.status_id', 1)
                  ->where('t2.status_id', 1)
                  ->get();

产出:

代码语言:javascript
复制
 [
        {
            "parent_id": 1,
            "parent_industry": "Parent Industry 1",
            "child_id": 2,
            "child_industry": "Child Industry 1",
            "parent_industry_id": 1
        },
        {
            "parent_id": 1,
            "parent_industry": "Parent Industry 1",
            "child_id": 3,
            "child_industry": "Child Industry 2",
            "parent_industry_id": 1
        },
        .
        .
        .
   ]

GroupBy parent_id没有工作。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-09-19 06:23:32

谢谢,我通过以下链接解决了这一问题:Laravel Eloquent Self Join Parent Child

代码语言:javascript
复制
public function parent()
{
    return $this->belongsTo(self::class, 'parent_industry_id');
}

public function children()
{
    return $this->hasMany(self::class, 'parent_industry_id');
}

public function getIndustries()
{  
    //return Industry::with('children')->get();
    return Industry::with(array('children'=>function($query){
                        $query->select('id','industry','parent_id');
                    }))
                    ->where('parent_id', null)
                    ->select('id','industry')
                    ->get();
}
票数 0
EN

Stack Overflow用户

发布于 2017-09-19 06:22:18

代码语言:javascript
复制
yourProjectBasePath/config/database.php

打开这个文件

代码语言:javascript
复制
'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

代码语言:javascript
复制
'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46293126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档