在laravel中将多个相同名称转换为一个单一名称

时间:2017-03-13 04:38:53

标签: php mysql model-view-controller laravel-5

这是控制器部分

package com.company;
import java.io.*;
import java.util.*;
import java.lang.*;

/**
 * Created by ayush on 12/3/17.
 */

public class Linked {

    public static void main(String args[]) throws IOException {

        BufferedReader br = new BufferedReader(
                new InputStreamReader(System.in));
        System.out.println("Enter the sentence");
        String st = br.readLine();
        st=st.trim();
        st = st + " ";
        int count = lengthx(st);
        System.out.println(count);
        String arr[] = new String[count];
        int p = 0;
        int c = 0;

        for (int i = 0; i < st.length(); i++) {
            if (st.charAt(i) == ' ') {
                arr[p] = st.substring(c,i);
                System.out.println(arr[p]);
                c = i + 1;
                p++;
            }
        }
        Map<String, Integer> map = new HashMap<>();

        for (String w : arr) {
            Integer n = map.get(w);
            n = (n == null) ? 1 : ++n;
            map.put(w, n);
        }
        for (String key : map.keySet()) {
            System.out.println(key + " = " + map.get(key));
        }

        Set<Map.Entry<String, Integer>> entries = map.entrySet();

        Comparator<Map.Entry<String, Integer>> valueComparator = new Comparator<Map.Entry<String,Integer>>() {

            @Override
            public int compare(Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2) {
                Integer v1 = e1.getValue();
                Integer v2 = e2.getValue();
                return v1.compareTo(v2); }
        };

        List<Map.Entry<String, Integer>> listOfEntries = new ArrayList<Map.Entry<String, Integer>>(entries);
        Collections.sort(listOfEntries, valueComparator);

        LinkedHashMap<String, Integer> sortedByValue = new LinkedHashMap<String, Integer>(listOfEntries.size());

        for(Map.Entry<String, Integer> entry : listOfEntries){

            sortedByValue.put(entry.getKey(), entry.getValue());
        }

        for(Map.Entry<String, Integer> entry : listOfEntries){

            sortedByValue.put(entry.getKey(), entry.getValue());
        }

        System.out.println("HashMap after sorting entries by values ");
        Set<Map.Entry<String, Integer>> entrySetSortedByValue = sortedByValue.entrySet();
        for(Map.Entry<String, Integer> mapping : entrySetSortedByValue){
            System.out.println(mapping.getKey() + " ==> " + mapping.getValue());
        }


    }

    static int lengthx(String a) {
        int count = 0;
        for (int j = 0; j < a.length(); j++) {
            if (a.charAt(j) == ' ') {
                count++;
            }
        }
        return count;
    }
}

这是我的观点

$users_list = User::where('id', '>', 4)
                ->where('status', 1)
                ->orderBy('id')
                ->get();

$companys = WorkFor::all();
$categories = TaskCetagorys::all();
//$report_table = array();

foreach($users_list as $user){

  $report_table = array();

  $user_tasks = Task::select('task_date',DB::raw('group_concat(tasks.workfor_name) as workfor_name'),'taskcategory_name',DB::raw('group_concat(tasks.description SEPARATOR ",,,") as description'))
                       ->wherebetween('task_date', array("$start_task_date", "$end_task_date"))
                            ->where('asign_id', '=', $user->id)
                            ->where('status', 1)
                            ->groupBy('workfor_id')
                            ->orderBy('task_date')
                            ->get();
 }

我正在使用 laravel 5.2 版本。在我的控制器中,我使用 group_concat 功能获取一个列数据。当我使用<显示这些数据时strong> view ,同一个名称连续多次重复。但我想只显示这些重复数据一次。例如,我使用group_concat从数据库中获取 workfor_name 。当我告诉他们 workfor_name 连续多次重复PALO,PALO,PALO,PALO,PALO,PALO,PALO,PALO,PALO。我想在我的行中只有一个PALO。需要帮助。 !!!

this is my view page

1 个答案:

答案 0 :(得分:0)

将关键字DISTINCT添加到您的GROUP_CONCAT来电。

$user_tasks = Task::select('task_date',DB::raw('group_concat(DISTINCT tasks.workfor_name) as workfor_name'),'taskcategory_name',DB::raw('group_concat(DISTINCT tasks.description SEPARATOR ",,,") as description'))
                   ->wherebetween('task_date', array("$start_task_date", "$end_task_date"))
                        ->where('asign_id', '=', $user->id)
                        ->where('status', 1)
                        ->groupBy('workfor_id')
                        ->orderBy('task_date')
                        ->get();