我正在循環收集並訪問模型上屬性,以便收集項目的用戶。Laravel - 獲取集合中每個集合的最新模型屬性
$advancedBookingsQuery = Booking::query();
$advancedBookingsQuery = $advancedBookingsQuery->where('type', 2)
->with('staffMember.user');
$advancedBookings = $advancedBookingsQuery->get(['id', 'requested_booking_time', 'estimated_booking_time']);
foreach ($advancedBookings as $booking) {
$barberQueueLength = $booking->staffMember->user->currentQueueLength;
$booking->update(['estimated_booking_time' => Carbon::now()->addMinutes($barberQueueLength)]);
}
currentQueueLength方法運行一些查詢以獲得符合要求的總預訂的長度。
public function getCurrentQueueLengthAttribute()
{
// Calc length of all bookings with an estimated booking time value
}
問題是currentQueueLength屬性總是返回集合中每個項目的相同值。
有沒有一種方法可以在集合的每次迭代中新調用該屬性 - >每種方法?它似乎只是在收集$ advancedBookings集合時使用該值。
每[Laravel訪問者&增變器(https://laravel.com/docs/5.5/eloquent-mutators#accessors -and-mutators),current_ueueLength是不是'current_queue_length'? – ljubadr