user(); if ($me->id === $user->id) { return response()->json(['error' => 'Cannot follow yourself'], 422); } if (!$me->following()->where('following_id', $user->id)->exists()) { $me->following()->attach($user->id); $user->notifications()->create([ 'id' => Str::uuid(), 'type' => 'new_follower', 'data' => json_encode([ 'follower_id' => $me->id, 'follower_slug' => $me->slug, 'follower_name' => $me->name, ]), 'created_at' => now(), ]); } return response()->json([ 'following' => true, 'count' => $user->followers()->count(), ]); } public function destroy(User $user): JsonResponse { auth()->user()->following()->detach($user->id); return response()->json([ 'following' => false, 'count' => $user->followers()->count(), ]); } }