0

i have an array of objects in laravel that i want to use where query on them so here is my code below :

  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 [▶]
  }
  1 => RoomPricingHistory {#506 ▶}
  2 => RoomPricingHistory {#513 ▶}
  3 => RoomPricingHistory {#520 ▶}
  4 => RoomPricingHistory {#527 ▶}
  5 => RoomPricingHistory {#534 ▶}
  6 => RoomPricingHistory {#541 ▶}
  7 => RoomPricingHistory {#548 ▶}
  8 => RoomPricingHistory {#555 ▶}
  9 => RoomPricingHistory {#562 ▶}
  10 => RoomPricingHistory {#569 ▶}
  11 => RoomPricingHistory {#576 ▶}
  12 => RoomPricingHistory {#583 ▶}
  13 => RoomPricingHistory {#590 ▶}
  14 => RoomPricingHistory {#597 ▶}
  15 => RoomPricingHistory {#604 ▶}
  16 => RoomPricingHistory {#611 ▶}
  17 => RoomPricingHistory {#618 ▶}
]

so here now for example i have 17 rooms here 5 with of 1, 5 with id of 2 , and 7 with id of 3 in this result and i want to get the last sales_price for each room so i in the end i would have 3 result with room id of 1 -2 -3 and with thier last price . just to mention i sorted them by date before the result

1
  • 2
    If you put array into collection, you can filter it with fluent helpers. Commented May 26, 2019 at 7:39

2 Answers 2

1

You can use this code for getting last record:

    for ($f = 0; $f < count($room_id); $f++) {
        for ($i = 0; $i < count($dates); $i++) {
            $roomprice[$f] = RoomPricingHistory::
            Where('accommodation_room_id', $room_id[$f])
            ->whereDate('from_date', '<=', $dates[$i])
            ->whereDate('to_date', '>=', $dates[$i])
            ->orderBy('created_at', 'desc')->first();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

let's say you have the data in the variable called $collection then you can execute the following code to filter data:

we first group data by id then select the last record.

$grouped = $collection->groupBy('id')->last();

$grouped->toArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.