我在 Laravel 中有一个对象数组,我想对它们使用 where 查询,所以这里是我的代码: array:18 [▼ 0 => RoomPricingHistory {#498 ▼ #fillable: array:19 [▶] #connection: "mysql" #table: "room_pricing_histories" #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:22 [▶] #original: array:22 [▼ "id" => 326 "accommodation_room_id" => 1 "net_price" => null "sales_price" => 3660000 "extra_bed_price" => null "half_charge_price" => null "half_board_price" => null "full_board_price" => null "foreign_net_price" => null "foreign_sales_price" => null "foreign_extra_bed_price" => null "foreign_half_charge_price" => null "foreign_half_board_price" => null "foreign_full_board_price" => null "operator_id" => 11 "commission_percent" => null "discount_percent" => 5.0 "from_date" => "2019-05-25 09:00:00" "to_date" => "2019-08-30 09:00:00" "is_deleted" => 0 "created_at" => "2019-05-25 09:00:00" "updated_at" => "2019-05-25 09:00:00" ] #changes: [] #casts: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: [] #touches: [] +timestamps: true #hidden: [] #visible: [] #guarded: array:1 [▶] }所以现在在这里,例如我有 17 个房间,其中 5 个房间为 1,5 个房间的 id 为 2,7 个房间的 id 为 3 在这个结果中,我想获得每个房间的最后一个 sales_price 所以我最终会有 3 个结果与房间 id 1 -2 -3 和他们的最后价格。只是提一下我在结果之前按日期对它们进行了排序
2 回答
杨__羊羊
TA贡献1943条经验 获得超7个赞
假设您在调用的变量中有数据,$collection那么您可以执行以下代码来过滤数据:
我们首先对数据进行分组,id然后选择最后一条记录。
$grouped = $collection->groupBy('id')->last();
$grouped->toArray();
- 2 回答
- 0 关注
- 162 浏览
添加回答
举报
0/150
提交
取消