cviebrock / eloquent-taggable

Easily add the ability to tag your Eloquent models in Laravel.
MIT License
559 stars 72 forks source link

Model::isTagged()->get() doesn't return any items in Laravel 5.4 #75

Closed yurivis closed 5 years ago

yurivis commented 6 years ago

I'm using this package to save tags associated with certain uses. Saving tags works without any issues. I have checked my database and confirmed that the tags are connected with the appropriate users. When I try to call User::isTagged()->get() it returns an empty collection. Using User::isNotTagged()->get() returns all users, even the ones that are tagged.

cviebrock commented 6 years ago

Sorry for the delayed reply.

Any chance you can log the DB queries being generated in both those cases and post them here? Adding something like DB::connection()->enableQueryLog(); before those calls, then dd(DB::getQueryLog()); afterwards should do it.

yurivis commented 6 years ago

@cviebrock

This is the queries I get:

For Profile::isTagged()->get(); select profiles.* from profiles inner join taggable_taggables on profiles.id = taggable_taggables.taggable_id and taggable_taggables.taggable_type = ? group by profiles.id

For Profile::isNotTagged()->get(); select profiles.* from profiles left join taggable_taggables on profiles.id = taggable_taggables.taggable_id and taggable_taggables.taggable_type = ? group by profiles.id having COUNT(DISTINCT taggable_taggables.taggable_id) = 0

Trying to execute the first SQL query returns results up intil and. adding the code between and and = ? returns no results. Executing the whole query returns an error.

Another thing I noticed, don't know if it's a bug or intended, in taggable_tags I get duplicates of the same tag multiple times. I'm saving the tags on Models using retag(). Maybe that causes the problem?

The problem above was caused by my incorrectly defined glue. The problem with isTagged() persists though.