Comments (8)
Hi @ssglopes,
You can use getQuery()
to get the underlying query builder and prevent the laravel_through_key
column from being added:
public function tags()
{
return $this->hasManyDeep(
Tag::class,
[Post::class, 'taggables'],
['author_id', ['taggable_type', 'taggable_id'], 'id'],
[null, null, 'tag_id']
);
}
$author->tags()
->getQuery()
->withoutGlobalScope(WithCountsScope::class)
->select('tags.id', 'tags.name')
->selectRaw('count(*) as "posts_count"')
->groupBy('tags.id', 'posts.author_id')
->get();
from eloquent-has-many-deep.
Hi @staudenmeir ,
thanks for your fast reply, that does indeed do the trick to get the correct query.
Only now I get this error:
"detail": "Call to undefined method Illuminate\\Database\\Eloquent\\Builder::addEagerConstraints()",
which is, i think, due to being now returned:
Illuminate\Database\Eloquent\Builder
instead of :
Staudenmeir\EloquentHasManyDeep\HasManyDeep
from eloquent-has-many-deep.
What does your eager loading query look like?
from eloquent-has-many-deep.
Hello @staudenmeir ,
The adjusted method by the recommendations you gave me causing the last above mentioned error now looks like this (regardless of its with and without ->get()
on the end). It throws the error so I can not check the query in telescope so i used toRawSql() to see the query.
API url: /roles/3/users?include=tags
public function tags()
{
$test = $this->hasManyDeep(
Tag::class,
[Post::class, 'taggables'],
['author_id', ['taggable_type', 'taggable_id'], 'id'],
[null, null, 'tag_id']
)
->getQuery()
->withoutGlobalScope(WithCountsScope::class)
//->withCustomThroughKeyCallback(fn() => ['tags.*', 'tags.id as laravel_through_key'])
->select('tags.id', 'tags.name')
->selectRaw('count(*) as "posts_count"')
//->groupByRaw('"tags"."id", "posts"."author_id"');
->groupBy('tags.id')
//->toRawSql()
->get()
;
//dd($test);
return $test;
}
The query outputted by toRawSql()
:
select
"tags"."id", "tags"."name", count(*) as "posts_count"
from
"tags"
inner join "taggables" on "taggables"."tag_id" = "tags"."id"
inner join "posts" on "posts"."id" = "taggables"."taggable_id"
where
"taggables"."taggable_type" = 'App\Models\Post'
and "tags"."deleted_at" is null
and "tags"."active" = 1
and "posts"."deleted_at" is null
group by "tags"."id"
If I call the endpoint again but with old method I wrote initially can see the actual query in telescope:
public function tags()
{
$test = $this->hasManyDeep(
Tag::class,
[Post::class, 'taggables'],
['author_id', ['taggable_type', 'taggable_id'], 'id'],
[null, null, 'tag_id']
)
//->getQuery()
->withoutGlobalScope(WithCountsScope::class)
->withCustomThroughKeyCallback(fn() => ['tags.*', 'tags.id as laravel_through_key'])
//->select('tags.id', 'tags.name')
->selectRaw('count(*) as "posts_count"')
//->groupByRaw('"tags"."id", "posts"."author_id"');
->groupBy('tags.id')
//->toRawSql()
//->get()
;
//dd($test);
return $test;
}
Query from telescope:
select
"tags"."id",
"tags"."name",
count(*) as "posts_count",
"posts"."author_id" as "laravel_through_key"
from
"tags"
inner join "taggables" on "taggables"."tag_id" = "tags"."id"
inner join "posts" on "posts"."id" = "taggables"."taggable_id"
where
"taggables"."taggable_type" = 'App\Models\Post'
and "posts"."author_id" in (2, 3)
and "tags"."deleted_at" is null
and "tags"."active" = 1
and "posts"."deleted_at" is null
group by
"tags"."id",
"posts"."author_id"
But the query I actually want to have should look like this:
select
"tags"."id",
"tags"."name",
count(*) as "posts_count"
from
"tags"
inner join "taggables" on "taggables"."tag_id" = "tags"."id"
inner join "posts" on "posts"."id" = "taggables"."taggable_id"
where
"taggables"."taggable_type" = 'App\Models\Post'
and "posts"."author_id" in (2, 3)
and "tags"."deleted_at" is null
and "tags"."active" = 1
and "posts"."deleted_at" is null
group by
"tags"."id"
So I do achieve with getQuery() to adjust the query but itself but then it fails becuase of the fatal error. Which I suspect its because the adjusted method with getQuery()
returns now a Builder instance instead of a Relation instance causing the fatal error to be trhown.
Thanks
from eloquent-has-many-deep.
Are you executing a query with ->with('tags')
or ->load('tags')
?
from eloquent-has-many-deep.
It hits this method https://github.com/laravel-json-api/eloquent/blob/01aa76ac3abcff97fd713e0bbf01c246f6dd1f5d/src/QueryBuilder/JsonApiBuilder.php#L227 which is a part of this package.
I put a dd() inside the below method and from that i can answer your question that it hits the ->with('')
public function with($includePaths): self
{
$includePaths = IncludePaths::nullable($includePaths);
$loader = new EagerLoader(
$this->schemas,
$this->schema,
$includePaths,
);
// ----------
dd($includePaths);
// ----------
$this->query->with($paths = $loader->getRelations());
foreach ($loader->getMorphs() as $name => $map) {
$this->query->with($name, static function(EloquentMorphTo $morphTo) use ($map) {
$morphTo->morphWith($map);
});
}
$this->eagerLoading = (!empty($paths) || !empty($map));
$this->parameters->setIncludePaths($includePaths);
return $this;
}
LaravelJsonApi\Core\Query\IncludePaths {#2956 // vendor/laravel-json-api/eloquent/src/QueryBuilder/JsonApiBuilder.php:232
-stack: array:1 [
0 =>
LaravelJsonApi\Core\Query
\
RelationshipPath {#2959
-names: array:1 [
0 => "tags"
]
}
]
}
from eloquent-has-many-deep.
Which I suspect its because the adjusted method with getQuery() returns now a Builder instance instead of a Relation instance causing the fatal error to be trhown.
Yes, the query is no longer relationship and can't be used for eager loading. I don't see a way to make your custom query with eager loading.
from eloquent-has-many-deep.
Ok thanks for your help regardless and will try to solve it in a different way.
from eloquent-has-many-deep.
Related Issues (20)
- Using with a Belongs To Many? HOT 1
- Data duplication on hasManyTrough with pivot HOT 3
- Relations not shown in artisan command model:show HOT 3
- Ambigious column reference 'id' HOT 1
- I can't get to conclude this relationship HOT 1
- HasOneTrhough MorphOne HOT 1
- issue with composite keys when doing a relation between 2 tables HOT 2
- Dynamic relationship property returning empty collection HOT 4
- collision with compoships HOT 1
- need help with hasManyDeep with belongsTo HOT 2
- distinct - paginate HOT 1
- Public Page -> belongsTo CompanyBranch -> morphMany Addresses HOT 1
- HasManyDeep Relation Issue HOT 2
- relations eager loading HOT 2
- I'm getting the results for all users - help please HOT 3
- Pivots not returned the same way HOT 3
- HasOneDeep is not generic HOT 5
- Laravel 11 HOT 4
- CompositeKey not included when installed. HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eloquent-has-many-deep.