普通网友 2019-01-10 23:02
浏览 200
已采纳

如何在Laravel中进行选择后对结果进行分组

I have a controller that returns me expiration_date and status of a ticket, referring an user_id.

After I get all tickets that an user has registered, the controller will show when expire and the status. Something like this:

0   
expiration_date "2018-03-03 20:38:32"
status  "expired"

1   
expiration_date "2018-03-03 20:38:32"
status  "expired"

2   
expiration_date "2018-03-03 20:38:32"
status  "expired"

3
expiration_date "2018-05-03 09:02:06"
status  "expired"

4
expiration_date "2019-05-03 09:02:06"
status  "available"

5   
expiration_date "2019-05-03 10:00:20"
status  "available"

My problem here is that I need to group the data that has the same expiration_date and status. So, I would group the data 0, 1, 2 and the data 4 and 5 in an unique item, counting the number of items. Something like this:

Count: 3
expiration_date "2018-03-03 20:38:32"
status  "expired"

Count: 1
expiration_date "2018-05-03 09:02:06"
status  "expired"

Count: 2
expiration_date "2019-05-03 10:00:20"
status  "available"

So, how do I do that? I'm using Laravel with mysql and here it is my Controller.

ControllerPHP

<?php

namespace App\Http\Controllers;

use DB;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UsersCreditsController extends Controller 
{

    public function getTicketsStatusByUserId($userId)
    {   

        $users = DB::select("
            SELECT COUNT(user_id)
             FROM users_credits 
             WHERE user_id = $userId
        ");

        foreach ($users as $user) {
          $item = DB::select("
              SELECT expiration_date, status
              FROM users_credits 
              WHERE user_id = $userId
              ORDER BY expiration_date ASC
          ");
          return $item;
        }
    }
}
  • 写回答

2条回答 默认 最新

  • duanjuelu8874 2019-01-13 01:57
    关注

    So, I remade my code using the eloquent that was indicated to me.

    <?php
    
    namespace App\Http\Controllers;
    
    use DB;
    use App\Http\Requests;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    
    class UsersCreditsController extends Controller 
    {
        public function getTicketsStatusByUserId($userId)
        {   
            $item = DB::table('users_credits')
                        ->select('expiration_date as Vencimento', 'status as Status', DB::raw('count(*) as Quantidade'))
                        ->where(function ($query) use ($userId)
                        {
                            $query->where('user_id', '=', $userId);
                            $query->whereNotIn('status', ['used']);
                        })
                        ->groupBy('expiration_date', 'status')
                        ->get();
            return $item;
        }
    }
    

    The code is working as intended, if anyone need to learn how to use this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里