douli6605 2017-01-13 15:15 采纳率: 100%
浏览 57

如何在导出.csv之前对php laravel上的数据进行排序?

I am trying to sort the datas before exporting them on excel. This is the code that I have been using:

public function getDashboardReport2(){
    (...)
        echo "<tr><td>Group Name</td><td>Unit</td><td>Unit Number</td><td>First Name</td><td>Last Name</td><td>Phone Number</td></tr>";

        ($group = \App\Group::find($auth_event->group_id);
                $bookings = \App\EventBookings::where("group_id", "=", $auth_event->group_id)->get();
                if($bookings->count() > 0){
                    foreach ($bookings as $booking){

                        $egroup = \App\EventGroup::find($booking->eg_id); // Here I tried orderBy('name');
                        $chalet = \App\Chalet::find($booking->chalet_id);
                            if($egroup) {
                            $egroup_members = \App\EventGroupMembers::where("eg_id", "=", $egroup->eg_id)->get();
                            $row = 0;
                                if ($egroup_members->count() > 0) {
                                    foreach ($egroup_members as $egroup_member) {
                                    $row++;
                                    $user = \App\User::find($egroup_member->user_id);
                                        if ($user) {
                                        $charges = \App\EventBookingCharges::where("user_id", "=", $user->user_id)
                                        ->where("booking_id", "=", $booking->booking_id)->get();
                                        $paid_amount = 0;
                                        $balance_amount = 0;
                                            if ($charges->count() > 0) {
                                                foreach ($charges as $charge) {
                                                    if ($charge->status == 1) {
                                                        $paid_amount = $paid_amount + $charge->amount;
                                                    } else {
                                                        $balance_amount = $balance_amount + $charge->amount;
                                                    }
                                                }
                                            })
                                echo "<tr>";

                                    if ($row == 1) {
                                    echo "<td>" . $egroup->name . "</td>"; // Sort using this data
                                    } else {
                                    echo "<td></td>";
                                    }

                                    if ($row == 1) {
                                    echo "<td>" . ($chalet ? $chalet->name : '') . "</td>";
                                    } else {
                                    echo "<td></td>";
                                    }

                                echo "</tr>";
                                }
                            }
                        echo "<tr><td colspan='9'><br></td></tr>";
                        }
                    }
                }
            }
        echo "</table>";
    }
}

How do I sort this one out? I have tried orderBy('name'); but it didn't work. I also tried searching it on google but all the tutorials are about sorting arrays.

  • 写回答

1条回答 默认 最新

  • dongwu8653 2017-01-13 16:02
    关注

    ok i think i understand now what you want to order. you want to order the bookings according to their egroups name!

    Im afraid I dont see an immediate answer for that unless you saved not only the eg_id in the bookings table but also the eg_name. then you could do something like

    $bookings = \App\EventBookings::where("group_id", "=", $auth_event->group_id)->orderBy('eg_name')->get();
    

    sorry for not being able to provide a better answer but i hope i could still be of help

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?