Skip to content

Table records are duplicated when using HasManyThrough #17104

@Iilun

Description

@Iilun

Package

filament/tables

Package Version

v4.0.0-beta19

Laravel Version

v12.21.0

Livewire Version

v3.6.4

PHP Version

PHP 8.4.8

Problem description

When a model contains a HasManyThrough relationship and it is used when overriding a resource getEloquentQuery(), items are duplicated based on the Through table id.

For example if i want to filter records by user on the list page using a HasManyThrough relationship.

Users -> Throughs -> Items 

The two main changes to the default app are the HasManyThrough relation in App\Models\User.php

public function items(): HasManyThrough 
{
    return $this->hasManyThrough(Item::class, Through::class);
}

And the override of getEloquentQuery in the resource

public static function getEloquentQuery(): Builder
{
    return Auth::user()->items()->getQuery();
}

Expected behavior

Items are not duplicated. It is a regression from v3, where this was working properly.

V3:

Image

V4:

Image

Steps to reproduce

I have provided a repository with a relevant README to reproduce.

Reproduction repository (issue will be closed if this is not valid)

https://github.com/Iilun/filament-v4-has-many-through

Relevant log output

When running tinker queries, we can find the root cause. When using HasManyThrough->getQuery()->get(), the `id` field from the model is indeed the same as `trough_id`.
What's interesting is that this is NOT the case when querying the relation directly (without getQuery)


> User::first()->items()->getQuery()->limit(2)->get()
= Illuminate\Database\Eloquent\Collection {#7082
    all: [
      App\Models\Item {#7072
        id: 1,
        through_id: 1,
        name: "Harley Davis",
        created_at: "2025-07-26 20:40:21",
        updated_at: "2025-07-26 20:40:21",
        user_id: 1,
      },
      App\Models\Item {#7083
        id: 1,
        through_id: 1,
        name: "Connie Carter",
        created_at: "2025-07-26 20:40:21",
        updated_at: "2025-07-26 20:40:21",
        user_id: 1,
      },
    ],
  }
> User::first()->items()->limit(2)->get()
= Illuminate\Database\Eloquent\Collection {#7056
    all: [
      App\Models\Item {#7057
        id: 1,
        through_id: 1,
        name: "Harley Davis",
        created_at: "2025-07-26 20:40:21",
        updated_at: "2025-07-26 20:40:21",
        laravel_through_key: 1,
      },
      App\Models\Item {#7103
        id: 2,
        through_id: 1,
        name: "Connie Carter",
        created_at: "2025-07-26 20:40:21",
        updated_at: "2025-07-26 20:40:21",
        laravel_through_key: 1,
      },
    ],
  }

Update:
It turns out this also induces a bug in v3, where the generated record link is for the through_id instead of item_id, even though the display is correct (the first three items all open page for id = 1)

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    Status

    Todo

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions