public function findEmptyCommentsVisitAssociated() { $visits_to_make = []; $comments = Comment::withTrashed()->whereBody('')->where('commentable_type', '=', 'Spot')->orWhereNull('body')->get()->each(function ($item) use(&$visits_to_make) { // do we have a visit ? $visit = Visit::withTrashed()->where('user_id', '=', $item->user_id)->where('spot_id', '=', $item->commentable_id)->get(); if ($visit->count() == 0) { array_push($visits_to_make, (object) ['user_id' => $item->user_id, 'spot_id' => $item->commentable_id, 'timestamp' => $item->created_at]); } }); foreach ($visits_to_make as $make) { $user = User::find($make->user_id); $spot = Spot::find($make->spot_id); if ($user && $spot) { $visit = new Visit(); $visit->timestamps = false; $visit->user()->associate($user); $visit->spot()->associate($spot); $visit->created_at = $visit->updated_at = $make->timestamp; $visit->save(); $visit->timestamps = true; $visit->fireActivityEvent(); } } $comments->each(function ($item) { $item->forceDelete(); }); return [$visits_to_make, $comments]; }