Find entries related to user and display field name If I’m finding all the entries that are related to a user, how could I get the field handle that the found user is referenced from?

{% set events = craft.entries.section('events').relatedTo(user) %}
{% for event in events %}

  {# What goes here? Hypothetical code based on non-existent method would be: #}

  {% set field = craft.fields.relatedTo(user) %}
  {{ field.title }}

{# Obviously this isn't possible. Any way to accomplish something like this? #}

{% endfor %}


There are multiple users fields in the found entries, and there will be different logic depending on which field the user is found.
What could work for you is to get the events for each field separatly:

{% set firstEvents = craft.entries.section('events').relatedTo({
    element: user,
    field: 'firstEntriesField'
}) %}

{% set secondEvents = craft.entries.section('events').relatedTo({
    element: user,
    field: 'secondEntriesField'
}) %}


If you need them combined in one loop, I can't think of another way other than to query x + 1 times:

{% set allEvents = craft.entries.section('events').relatedTo({
    element: user,
}) %}

{% set firstEventsIds = craft.entries.section('events').relatedTo({
    element: user,
    field: 'firstEntriesField'
}).ids() %}

{% set secondEventsIds = craft.entries.section('events').relatedTo({
    element: user,
    field: 'secondEntriesField'
}).ids() %}

{% for event in allEvents %}
    {% if event.id in firstEventsIds %}
        {# Event related through firstEntriesField #}
    {% elseif event.id in secondEventsIds %}
        {# Event related through secondEntriesField #}
    {% endif %}
{% endfor %}