# `Backpex.Fields.HasManyThrough`
[🔗](https://github.com/naymspace/backpex/blob/0.18.3/lib/backpex/fields/has_many_through.ex#L2)

A field for handling a `has_many` (`through`) relation.

This field is not orderable or searchable.

> #### Warning {: .warning}
>
> This field is in beta state. Use at your own risk.

## Field-specific options

See `Backpex.Field` for general field options.

* `:display_field` (`t:atom/0`) - Required. The field of the relation to be used for displaying options in the select.

* `:live_resource` (`t:atom/0`) - Required. The corresponding live resource of the association. Used to display the title of the modal and generate defaults
  for `:child_fields` fields.

* `:sort_by` (list of `t:atom/0`) - A list of columns by which the child element output will be sorted. The sorting takes place in ascending order.

* `:child_fields` (`t:keyword/0`) - WIP

* `:pivot_fields` (`t:keyword/0`) - List to map additional data of the pivot table to Backpex fields.

* `:options_query` (function of arity 2) - Manipulates the list of available options in the select. Can be used to select additional data for the `display_field` option or to limit the available entries.",

  Defaults to `fn (query, _field) -> query end` which returns all entries.

## Example

    @impl Backpex.LiveResource
    def fields do
    [
      addresses: %{
        module: Backpex.Fields.HasManyThrough,
        label: "Addresses",
        display_field: :street,
        live_resource: DemoWeb.AddressLive,
        sort_by: [:zip, :city],
        pivot_fields: [
          type: %{
            module: Backpex.Fields.Select,
            label: "Address Type",
            options: [Shipping: "shipping", Billing: "billing"]
          }
        ]
      }
    ]
    end

The field requires a [`Ecto.Schema.has_many/3`](https://hexdocs.pm/ecto/Ecto.Schema.html#has_many/3) relation with a mandatory `through` option in the main schema. Any extra column in the pivot table besides the relational id's must be mapped in the `pivot_fields` option or given a default value.

# `config_schema`

Returns the schema of configurable options for this field.

This can be useful for reuse in other field modules.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
