コレクションアイテムをループして、コレクションアイテムのユーザーのモデルの属性にアクセスしています。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コレクションが収集されたときの値を使用しているようです。
私はまたリフレッシュ方法を検討してみてください、ちょうど昨日、同様の問題に遭遇した代わりに$booking->staffMember->user->currentQueueLength;
を使用しての
$booking->staffMember()->first()->user()->first()->currentQueueLength;
を使用してみてください
当たり[Laravelアクセッサとミューテータ(https://laravel.com/docs/5.5/eloquent-mutators#accessors - と - mutators)、 'currentQueueLength'は' current_queue_length'であってはなりませんか? – ljubadr