Laravel Eloquent Group在自我加入时不工作

时间:2017-09-19 06:14:17

标签: php database postgresql laravel eloquent

我有一张表说具有以下结构的行业:

 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   

现在,我需要在模型中将其显示为parent-> children对象输出。 我可以加入并获取详细信息,但GroupBy子句似乎无法正常工作。仍然获得平坦的层次结构输出

我的查询:

 $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();

输出:

 [
        {
            "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无效。

2 个答案:

答案 0 :(得分:0)

yourProjectBasePath/config/database.php

打开此文件并

'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,
        ],

'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,
        ],

答案 1 :(得分:0)

谢谢, 通过以下链接,我能够通过Laravel关系来解决这个问题: Laravel Eloquent Self Join Parent Child

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();
}