Skip to content

Conversation

@inxilpro
Copy link
Contributor

This is a revised version of #37956 that also brings in the new tests from #40402. Instead of adding any methods to the interface, I've added @mixin annotations. I've also added two contracts to address the differences between Eloquent queries and base queries.

As a result, you can do the following:

use Illuminate\Contracts\Database\Query\Builder;

return Model::query()
  ->whereNotExists(function(Builder $query) {
    // no need to know that $query is a Query\Builder
  })
  ->whereHas('relation', function(Builder $query) {
    // no need to know that $query is an Eloquent\Builder
  })
  ->with('relation', function(Builder $query) {
    // no need to know that $query is an Eloquent\Relation
  });

If you need to use Eloquent-specific features, then you need to type-hint the Eloquent builder:

use Illuminate\Contracts\Database\Eloquent\Builder as Eloquent;
use Illuminate\Contracts\Database\Query\Builder;

return Model::query()
  ->whereNotExists(function(Builder $query) {
    // only do base builder operations here
  })
  ->whereHas('relation', function(Eloquent $query) {
    // maybe I need another whereHas() here
  });

The @mixin annotations provide good IDE support without needing to change the existing Builders at all:

image

I hope this works out as a good compromise for the 9.x release!

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