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

时间:2017-01-13 15:15:12

标签: php laravel csv

我尝试在将数据导出到excel之前对数据进行排序。这是我一直在使用的代码:

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>";
    }
}

如何对此进行排序?我试过了orderBy('name');,但它没有用。我也尝试在谷歌搜索它,但所有的教程都是关于排序数组。

1 个答案:

答案 0 :(得分:0)

好吧,我想我现在明白你想要点什么。你想根据他们的egroups名称订购预订!

我担心除非您不仅保存了预订表中的eg_id,还保存了eg_name,否则我不会立即看到答案。然后你可以做类似

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

抱歉无法提供更好的答案,但我希望我仍能提供帮助