如何在Laravel模型类中实现GROUP_CONCAT?

时间:2018-05-21 09:44:21

标签: mysql laravel

我使用GROUP_CONCAT功能为Laravel中选定的专业人员提取多张照片图像路径。代码在模型类中。 代码段如下:

$professionals = DB::table('member_professional')
    ->select("member_professional.*",
        DB::raw("(GROUP_CONCAT(DISTINCT photos.picture_path SEPARATOR ',')) as 'photos'"))
    ->join('member','member_professional.member_id_fk','=','member.member_id')
    ->join('photos','photos.member_fk','=','member.member_id')
    ->leftjoin('locations','member.city','=','locations.location_id');

        if ($professionals_type != '') {
                     $professionals = $professionals->where('member_professional.biz_category','=',$professionals_type);
        }

        if ($location != '') {
                     $professionals = $professionals->where('member.city','=',$location);
        }

    $professionals = $professionals->where('member.member_type','=',2); 
    $professionals = $professionals->groupBy('member_professional.member_id_fk')
    ->paginate(20);

当我在登台服务器上运行它时:http://dev.interioradditions.pk/allProfessionals它会抛出错误:

  

SQLSTATE [42000]:语法错误或访问冲突:1055' interior_dev_db.member_professional.biz_name'不是   GROUP BY(SQL:选择member_professional。*,(GROUP_CONCAT(DISTINCT photos.picture_path SEPARATOR','))作为照片'   来自member_professional上的member内部加入member_professionalmember_id_fk = membermember_id内部加入photos photosmember_fk = membermember_id   在locations上左加入membercity = locationslocation_id   其中membermember_type = 2   按member_professional分组。member_id_fk限制20偏移0)

当我从上面的语句中提取构建的sql查询并通过PHPMyAdmin执行时,它运行正常,并按预期提供结果。请帮助我们解决模型查询中的错误。

谢谢!

2 个答案:

答案 0 :(得分:1)

我喜欢通过定义你的模型和它们之间的关系来通过laravel的方法

class MemberProfessional extends Model
{  
    public function member()
    {
        return $this->hasMany('App\Member', 'member_id', 'member_id_fk');
    }
}

class Member extends Model
{
    public function photos()
    {
        return $this->hasMany('App\Photos', 'member_fk', 'member_id');
    }    
    public function locations()
    {
        return $this->hasMany('App\Locations', 'location_id', 'city');
    }   
    public function member_professional()
    {
        return $this->belongsTo('App\MemberProfessional', 'member_id_fk');
    }   
}

class Photos extends Model{
//...
}

class Locations extends Model{
//...
}

然后你可以编写简单的雄辩查询

$professionals = MemberProfessional::with('member.photos');

if ($professionals_type != '') {
    $professionals = $professionals->where('biz_category','=',$professionals_type);
}

if ($location != '') {
    $professionals->whereHas('member', function ($query) use ($location) {
                $query->where('city', '=', $location);
                  });
}
    $professionals->whereHas('member', function ($query) use ($location) {
                $query->where('member_type', '=', 2);
                  })
                  ->paginate(20);

现在,对于每一行,您将收集相关的Photos模型对象

答案 1 :(得分:0)

根据新版本ONLY_FULL_GROUP_BY模式默认启用,您需要使用完整的GROUP BY表达式,例如您的选择列表中存在的所有列(聚合的列除外)应存在于{{1}中喜欢

GROUP BY

在您的情况下,您需要指定select a,b, count(c) from table group by a,b 所需的列

member_professional
  

另请注意使用GROUP_CONCAT"结果被截断为group_concat_max_len系统变量给出的最大长度,该变量的默认值为1024.尽管有效最大长度为返回值受max_allowed_pa​​cket"。

的约束