我正在循环收集并访问模型上属性,以便收集项目的用户。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