【问题标题】:Sort order laravel eloquent relation排序顺序 laravel 雄辩关系
【发布时间】:2018-11-07 16:28:07
【问题描述】:

我面临排序 laravel 集合的问题。 我创建了如下查询。

 $schoolBookings = SchoolModel::with(['bookings.crts',
                        'bookings.crts.users' => function($que) {
                            $que->orderBy('first_name', 'ASC');
                        }
                        , 'schoolType',
                        'bookings.crts.teachingSpecializations',
                        'bookings' => function($que) use($filters, $input) {
                            if (!empty($input['start_date'])) {
                                $que->where('booked_on', '>=', $input['start_date']);
                            }
                            if (!empty($input['end_date'])) {
                                $que->where('booked_on', '<=', $input['end_date']);
                            }
                            $que->where('booked_on', '<=', date("Y-m-d 00:00:00"));
                            $que->orderBy('booked_on', 'desc');
                        },])
                    ->where(function($que) use ($filters) {
                        if (!empty($schoolId)) {
                            $que->where('id', $schoolId);
                        }
                    })
                    ->get()->toArray();

正如您显示的通过预订进行的排序工作正常。 但我想根据预订日期排序。然后按 CRT 用户名 这是我得到的。

Booking_date,  booking_id  USER_NAME    day
29/05/2018 -- 392514 --  Ehsanullah Islami Full day 
28/05/2018 -- 881393 --  Patricia Louden Full day 
28/05/2018 -- 848449 --  Ehsanullah Islami Full day
28/05/2018 -- 438674 --  Kirsten Wheeler Full day 
28/05/2018 -- 692704 --  Juliete Reim Full day
28/05/2018 -- 594508 --  Kasandra Kulas Full day 
28/05/2018 -- 737695 --  Roland Clements Full day 

任何人都可以建议如何通过预订而不是 CRT 用户名按字母顺序排序。

谢谢。

【问题讨论】:

  • 你试过用这个$que-&gt;orderBy('Booking_date', 'ASC');吗?
  • 是的,在此之后我发现结果按日期排序。正如我在问题中提到的那样..

标签: mysql laravel orm eloquent


【解决方案1】:

你可以试试下面的,查询完成后会排序。

 $schoolBookings = SchoolModel::with(['bookings.crts',
                        'bookings.crts.users' => function($que) {
                            $que->orderBy('first_name', 'ASC');
                        }
                        , 'schoolType',
                        'bookings.crts.teachingSpecializations',
                        'bookings' => function($que) use($filters, $input) {
                            if (!empty($input['start_date'])) {
                                $que->where('booked_on', '>=', $input['start_date']);
                            }
                            if (!empty($input['end_date'])) {
                                $que->where('booked_on', '<=', $input['end_date']);
                            }
                            $que->where('booked_on', '<=', date("Y-m-d 00:00:00"));
                            $que->orderBy('booked_on', 'desc');
                        },])
                    ->where(function($que) use ($filters) {
                        if (!empty($schoolId)) {
                            $que->where('id', $schoolId);
                        }
                    })
                    ->get()->sortBy('bookings.booked_on')->toArray()

【讨论】:

    猜你喜欢
    • 2017-04-11
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    相关资源
    最近更新 更多