简体   繁体   中英

How to hide repetitive data while retrieving data from database

I have this function:

public function readData(){
$TableB1 = \DB::table('users')
->join('group_user', 'users.id', '=', 'group_user.user_id')
->join('groups', 'groups.id', '=', 'group_user.group_id')
->join('meetings', 'users.id', '=', 'meetings.owned_by_id')
->select(
    'users.name as name',
    'group_user.user_id as id',
    'groups.name as groupname',
    'meetings.owned_by_id as meetings'
)
->groupBy('users.id')
->get();

I get this error :

在此处输入图片说明

When I remove the groupBy part it works but repetitive data like this it returns data like this :

[{"name":"Mohamed Adel","id":6,"groupname":"Team Mohamed","meetings":6},{"name":"Mohamed Adel","id":6,"groupname":"Team Mohamed","meetings":6},{"name":"Norman Osborn","id":2,"groupname":"Team Mohamed","meetings":2},{"name":"Harry Osborn","id":3,"groupname":"Team Harry","meetings":3}]

Thank you.

Use This

public function readData()
{
    $TableB1 = \DB::table('users')
        ->join('group_user', 'users.id', '=', 'group_user.user_id')
        ->join('groups', 'groups.id', '=', 'group_user.group_id')
        ->join('meetings', 'users.id', '=', 'meetings.owned_by_id')
        ->select(
            'users.id as user_id',
            'users.name as name',
            'group_user.user_id as id',
            'groups.name as groupname',
            'meetings.owned_by_id as meetings'
        )
        ->groupBy('user_id')
        ->get();

Change config\\database.php to

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', 'localhost'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
    'strict' => false, // must be false
    'engine' => null
],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM