/** * get date of last time this song was used in a plan */ public function getLastTimeUsedAttribute($value) { $id = $this->id; // get list of plans using this song $plan = Plan::whereHas('items', function ($query) use($id) { $query->where('song_id', $id)->where('date', '<', Carbon::now()); })->orderBy('date', 'desc')->first(); if ($plan) { return $plan->date; } return null; }