有人可以在Laravel中将此PHP代码转换为类似的内容。
$result = mysqli_query($conn, "SELECT birthday, count(*) FROM person group by birthday;");
while ($row = mysqli_fetch_array($result, MYSQL_NUM)) {
$yearvalues[$row[0]] = (int)$row[1];
}
像这样:
$female = Enroll::select(DB::raw("SUM(tot_enroll) as count"))
->orderBy(DB::raw('sy'))
->groupBy(DB::raw("(sy)"))
->where('gender','=', 'Female')
->get()->toArray();
$female = array_column($female, 'count');
答案 0 :(得分:1)
这很简单,
您可以将您的sql查询转换为laravel中的查询构建器,如下所示:
DB::table('person')->select('birthday',DB::raw('COUNT(*) as count'))
->groupBy('birthday')
->get();
在您的情况下,您忘记选择sy
和gender
,因此orderBy
,groupBy
和where
不执行任何操作,请尝试使用此代码获取数据
DB::table('enrollments')->select(DB::raw('sy','gender','SUM(tot_enroll) as count'))
->where('gender','=', 'Female')
->groupBy('sy')
->orderBy('sy')
->get();
->toArray();
答案 1 :(得分:0)
我假设您的型号名称是User。然后你可以这样写。
User::select('birthday')->groupBy('person')->get()->count;