Skip to content

Conversation

@kettasoft
Copy link
Owner

Summary

This PR replaces all usages of:

use Illuminate\Database\Eloquent\Builder;

with:

use Illuminate\Contracts\Database\Eloquent\Builder;

across the entire package.

The change ensures that the package can accept any Eloquent builder–compatible instance, not only the base Illuminate\Database\Eloquent\Builder.


Motivation

The previous implementation strictly type-hinted Illuminate\Database\Eloquent\Builder, which caused type conflicts when passing relationship builders such as HasMany, HasOne, or BelongsTo into Filterable::setBuilder().

Example of the issue:

$hasMany = User::find(1)->posts();   // This returns a HasMany instance
Filterable::create()->setBuilder($hasMany);

This throws an error because:

  • posts() returns an instance of Illuminate\Database\Eloquent\Relations\HasMany
  • HasMany implements the contract Illuminate\Contracts\Database\Eloquent\Builder
  • but does not extend the concrete class Illuminate\Database\Eloquent\Builder
  • therefore the type-hint rejected it as invalid, even though it is fully compatible from Laravel’s perspective.

Why the Contract is More Appropriate

Laravel relationship builders implement the contract, not the concrete Builder class.
By switching to the contract:

  • We support all Eloquent builder variations.
  • The package becomes compatible with relationship queries.
  • setBuilder() can accept query builders returned from relationships.
  • We avoid forcing consumers to manually access $hasMany->getQuery() or similar workarounds.

This makes the package behave consistently with Laravel's own expectations regarding builder polymorphism.


Result

After this change:

$hasMany = User::find(1)->posts();
Filterable::create()->setBuilder($hasMany);

works as expected without throwing binding or instantiation errors.


Additional Notes

  • No breaking changes for end-users that already rely on normal model builders.
  • Provides broader compatibility for advanced Eloquent usage, especially in complex domain layers.
  • Aligns the package more closely with Laravel’s internal type system and contracts-first design philosophy.

@kettasoft kettasoft merged commit 7609b3f into master Dec 4, 2025
1 check passed
@kettasoft kettasoft deleted the fix/type-hint branch December 4, 2025 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants