在Laravel

时间:2018-05-28 17:40:00

标签: laravel group-by fetch distinct

我被要求从单个表中加载数据,如下所示:

    =================================
    |id |dept | response | created_at |
     =================================
    |1  |FO   | GOOD     | 2018-05-20 |
    |2  |FO   | GOOD     | 2018-05-20 |
    |3  |IT   | GOOD     | 2018-05-20 |
    |4  |FO   | BAD      | 2018-05-20 |
    |5  |IT   | GOOD     | 2018-05-20 |
    |6  |LO   | BAD      | 2018-05-20 |
    |7  |IT   | GOOD     | 2018-05-20 |
    |8  |IT   | GOOD     | 2018-05-21 |
    |9  |LO   | GOOD     | 2018-05-21 |
     =================================

他们希望我只显示created_at 2018-05-20的记录。刀片中所需的输出显示如下:

 FO
    GOOD    2
    BAD     1
 IT
    GOOD    3
    BAD     0
 LO
    GOOD    0
    BAD     1

当我使用select distinct时,它只显示一行响应数据(仅GOOD)。当我使用groupBy

时会发生同样的事情

这是我目前的代码:

控制器:

$date =  new Carbon('2018-05-20');

$result = Result::select('dept', 'response')->where('created_at', '=', $date->toDateString())->groupby('dept')->get();

刀片:

 @foreach($result as $result)
    {{$result->dept}}</BR>

    {{$result->response}}</br></br>
@endforeach

我得到的输出:

 FO
 GOOD    

 IT
 GOOD    

 LO
 GOOD    

我还没有count,因为我仍然得到了这个不受欢迎的结果。 有没有办法用这个表来实现所需的请求?

编辑I: 基于Yrv16's答案,我的控制器是这样的:

namespace App\Http\Controllers;
use App\Result;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

public function index()
{

$date =  new Carbon('2018-05-20');

$results = Result::select('dept', 'response', \DB::raw('COUNT(*) as count'))
      ->where('created_at', '=',$date->toDateString())
      ->groupby('dept','response')              
      ->get();

$results = $results->groupBy('dept');
return view('result.index', compact ('results'));
}

和我的刀片:

@foreach($results as $result)
{{$result->dept}}</BR>

    {{$result->response}}</br></br>
@endforeach

这给了我一个空页面。但当我将此where('created_at', '=', $date->toDateString())更改为where('created_at', '>=', $date->toDateString())时,我收到此错误Property [dept] does not exist on this collection instance.

2 个答案:

答案 0 :(得分:2)

groupBy用于两列deptresponse

$date =  new Carbon('2018-05-20');

$results = Result::select('dept', 'response', \DB::raw('COUNT(*) as count'))
          ->where('created_at', '=',$date->toDateString())
          ->groupby('dept','response')              
          ->get();

结果收集你可以groupBy('dept')以便按照你想要的方式显示它:

$results = $results->groupBy('dept');

刀片:

@foreach($results as $dept => $result)
    {{$dept}}</BR>
    @foreach($result as $item)
        {{$item->response}} {{ $item->count }}
    @endforeach
@endforeach

答案 1 :(得分:0)

您可以使用集合操作:

$results = Result::where('created_at', '2018-05-20')
    ->get()
    ->groupBy('dept')
    ->map(function ($item) {
        return $item->groupBy('response')->map(function ($responses) {
            return $responses->count();
        });
    });

它会给你以下结果:

print_r($results->toArray());

Array (
[FO] => Array (
        [GOOD] => 2
        [BAD] => 1
    )
[IT] => Array (
        [GOOD] => 3
    )
[LO] => Array (
        [BAD] => 1
    )
)