【问题标题】:How do I count male, female and total students from Student table based on Class level in laravel using eloquent?如何使用 eloquent 根据 laravel 中的班级级别计算学生表中的男性、女性和总学生?
【发布时间】:2020-07-04 07:04:53
【问题描述】:

如何使用 eloquent 根据 laravel 中的班级级别计算学生表中的男性、女性和总学生数。我想创造这样的东西;

==================================================
| year     |  Male    |   Female   |  Total     |
==================================================
|Level 100 |   60     | 70         |     130    |
|Level 200 |  23      | 20         |     43     |
|Level 300 |  23      | 35         |     58     |
================================================

我试过这个, 这是我的控制器(我从 AppServiceProvider.php 共享数据)

$countgender = DB::table('students')
                 ->select('students.gender', DB::raw('count(*) as total'))
                 ->groupBy('c_level')
                 ->get();
                 $view->with('gender', $countgender);

下面的刀片模板

<table class="table">
  <thead>
    <tr>
      <th scope="col" width="50%">Year Group</th>
      <th scope="col">Male</th>
      <th scope="col">Female</th>
      <th scope="col">Total</th>
    </tr>
  </thead>
  <tbody>
    @foreach($gender as $gender)
    <tr>
      <th scope="row">{{$gender->c_level}}</th>
      <td>{{$gender->total}}</td>
      <td>{$gender->total}}</td>
      <td></td>
    </tr>
    @endforeach
  </tbody>
</table>

另外,这就是我的表在我的数据库中的结构。

public function up()
{
    Schema::create('students', function (Blueprint $table) {
        

        $table->id('std_id');
        $table->unsignedBigInteger('student_reference_table');
        $table->string('Programme');
        $table->string('c_level');
        $table->string('image')->nullable();
        $table->string('surname');
        $table->string('f_name');
        $table->string('f_email');
        $table->string('DoB');
        $table->string('p_address');
        $table->string('nationality');
        $table->string('region');
        $table->string('home_town');
        $table->string('phone');
        $table->string('mobile');
        $table->string('p_school');
        $table->string('p_location');
        $table->string('disability');
        $table->string('gender');
        $table->string('fee_category');
        $table->string('religion');
        $table->string('title');
        $table->string('g_surName');
        $table->string('g_fname');
        $table->string('occupation');
        $table->string('gp_address');
        $table->string('p_region');
        $table->string('g_mobile');
        $table->string('g_mail');
        $table->string('std_status')->nullable();
        $table->timestamps('deleted_at')->nullable();
        $table->timestamps();

        $table->foreign('student_reference_table')
        ->references('student_reference')
        ->on('users')
        ->onDelete('cascade');
        
    });
}

【问题讨论】:

  • 请编辑您的帖子以与示例数据共享表格。您分享的那个没有c_levelgender
  • 请给我,我只是想知道怎么做
  • 请帮助我
  • 我正在尝试,但您需要为现有的表结构提供示例数据。您刚刚分享了您的预期输出。
  • 请我更新问题

标签: php mysql laravel eloquent


【解决方案1】:

您可以使用WITH ROLLUP 来获取按行分组的总数。

SELECT `c_level`, IFNULL(gender, 'total') AS gender, count(*) AS sum
FROM `students`
GROUP BY `c_level`, `gender`
WITH ROLLUP

这是查询生成器版本;

return DB::table('students')
    ->groupBy(DB::raw('c_level, gender WITH ROLLUP'))
    ->get(['c_level', DB::raw('IFNULL(gender, "total") AS gender') , DB::raw('count(*) as sum')])
    ->reject(function ($row) {
        return !$row->c_level; // remove roll-up null value
    })
    ->groupBy('c_level'); // this one is collection's group by

它会打印出类似这样的东西; (每个级别的值将是集合的关键)。然后,您可以使用两个嵌套循环在前端进行迭代。第一个循环是年份(行),第二个是性别(列)的值。

{
    "100": [{
        "c_level": "100",
        "gender": "female",
        "sum": 8
    }, {
        "c_level": "100",
        "gender": "male",
        "sum": 2
    }, {
        "c_level": "100",
        "gender": "total",
        "sum": 10
    }],
    "120": [{
        "c_level": "120",
        "gender": "female",
        "sum": 1
    }, {
        "c_level": "120",
        "gender": "male",
        "sum": 1
    }, {
        "c_level": "120",
        "gender": "total",
        "sum": 2
    }],
    "150": [{
        "c_level": "150",
        "gender": "male",
        "sum": 2
    }, {
        "c_level": "150",
        "gender": "total",
        "sum": 2
    }],
    "200": [{
        "c_level": "200",
        "gender": "female",
        "sum": 2
    }, {
        "c_level": "200",
        "gender": "male",
        "sum": 2
    }, {
        "c_level": "200",
        "gender": "total",
        "sum": 4
    }]
}

【讨论】:

  • 我厌倦了循环使用@foreach,但它不起作用。你能帮我解决这个问题吗?我实际上是新人,发现很难做某事。谢谢。
  • @PrinceAsamoah 我说你需要两个循环并为其编写说明。问题是关于如何用 eloquent 来查询这个 - 我试图提供它。不熟悉刀片。
猜你喜欢
  • 2013-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多