在laravel的分离器

时间:2017-12-06 05:40:52

标签: php database laravel

我正在尝试在我的连接语句中使用seperator,但它继续使用不起作用。我尝试了一些网站和问题,但仍然无法正常工作。有人可以指导我哪里出错了吗?可能是因为我使用它的方式吗?

以下是代码:

$data = PersonalInfo::join('evaluations','evaluations.user_id', '=', 'personal_infos.id')
-> join('qualifications', 'personal_infos.id', '=', 'qualifications.user_id') 
-> join( 'language_proficiencies', 'personal_infos.id', '=', 'language_proficiencies.user_id')
-> join( 'families', 'personal_infos.id', '=', 'families.user_id')


    ->select( 
      'language_proficiencies.meta_value as Language',
      'qualifications.meta_value as Education',
      'families.meta_value as Immediate Family',
      'personal_infos.name as Name',
      'personal_infos.created_at as Date')
    ->whereDate('personal_infos.created_at', '=', date('Y-m-d')) 
    ->where('evaluations.Evaluation_Status', '=', 'Yes') 
    ->where('language_proficiencies.meta_key' , '=', 'Language_Name') 
    ->where('qualifications.meta_key' , '=', 'qualification_list') 
    ->where('families.meta_key' , '=', 'relationshiplist') 
    ->get()
    ->sortByDesc('created_at');

我做了类似这样的事情,但它不起作用

   // DB::raw("(GROUP_CONCAT(language_proficiencies.meta_value SEPARATOR ', ')) as 'Language'",
     // DB::raw("(GROUP_CONCAT(qualifications.meta_value SEPARATOR ', ')) as 'Education'",
       // DB::raw("(GROUP_CONCAT(families.meta_value SEPARATOR ', ')) as 'Immediate Family'",

     // GROUP_CONCAT(language_proficiencies.meta_value SEPARATOR ', ') as 'Language',
      //GROUP_CONCAT(qualifications.meta_value SEPARATOR ', ') as 'Education',
      //GROUP_CONCAT(families.meta_value  SEPARATOR ', ') as 'Immediate Family',

编辑: 只想在资格,语言缺陷和家庭上做分隔符,我删除了一些代码以防止它太乱。

1 个答案:

答案 0 :(得分:1)

要使用GROUP_CONCAT,您应该使用GROUP BY子句,因为它是一个聚合函数,使用分组条件,您的整个表将被视为一个组,并且所有行的值连接为逗号分隔列表或包含已定义分隔符的列表

$data = PersonalInfo::join('evaluations','evaluations.user_id', '=', 'personal_infos.id')
    -> join('qualifications', 'personal_infos.id', '=', 'qualifications.user_id') 
    -> join( 'language_proficiencies', 'personal_infos.id', '=', 'language_proficiencies.user_id')
    -> join( 'families', 'personal_infos.id', '=', 'families.user_id')
    ->select( 
      DB::raw("(GROUP_CONCAT(language_proficiencies.meta_value SEPARATOR ', ')) as Language"),
      DB::raw("(GROUP_CONCAT(qualifications.meta_value SEPARATOR ', ')) as Education"),
      DB::raw("(GROUP_CONCAT(families.meta_value SEPARATOR ', ')) as ImmediateFamily"),
      'personal_infos.name as Name',
      'personal_infos.created_at as Date')
    ->whereDate('personal_infos.created_at', '=', date('Y-m-d')) 
    ->where('evaluations.Evaluation_Status', '=', 'Yes') 
    ->where('language_proficiencies.meta_key' , '=', 'Language_Name') 
    ->where('qualifications.meta_key' , '=', 'qualification_list') 
    ->where('families.meta_key' , '=', 'relationshiplist') 
    ->groupBy('personal_infos.name')
    ->groupBy('personal_infos.created_at')
    ->get()
    ->sortByDesc('created_at');
  

另请注意,根据docs 使用GROUP_CONCAT对字符限制进行限制。结果被截断为group_concat_max_len系统变量给出的最大长度,该变量的默认值为1024。值可以设置得更高,尽管返回值的有效最大长度受max_allowed_pa​​cket

的值约束