cviebrock/eloquent-taggable

Problem about withAllTags() and select()

codesinging opened this issue · 2 comments

When:
$project->where('status', true)->select(['id', 'created_at'])->withAllTags('vue')->get()
Query SQL:

select
  `projects`.*
from
  `projects`
  inner join `taggable_taggables` as `taggable_taggables_scopewithalltags_1` on `projects`.`id` = `taggable_taggables_scopewithalltags_1`.`taggable_id`
  and `taggable_taggables_scopewithalltags_1`.`taggable_type` = 'App\Models\Project'
where
  `status` = 1
  and `taggable_taggables_scopewithalltags_1`.`tag_id` in (1)
group by
  `projects`.`id`
having
  COUNT(
    DISTINCT taggable_taggables_scopewithalltags_1.tag_id
  ) = 1

The select(['id', 'created_at']) does not work.

When:
$project->where('status', true)->withAllTags('vue')->select(['id', 'created_at'])->get()

Exception:
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in field list is ambiguous (SQL: select `id`, `created_at` from `projects` inner join `taggable_taggables` as `taggable_taggables_scopewithalltags_1` on `projects`.`id` = `taggable_taggables_scopewithalltags_1`.`taggable_id` and `taggable_taggables_scopewithalltags_1`.`taggable_type` = App\Models\Project where `status` = 1 and `taggable_taggables_scopewithalltags_1`.`tag_id` in (1) group by `projects`.`id` having COUNT(DISTINCT taggable_taggables_scopewithalltags_1.tag_id) = 1) in file /Users/zhangrui/sites/jscss-api/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 692

I think I have a similar problem.
I have select('sites.*', 'subscriptions.stripe_status').
But when I add withAnyTags('tag') I lose the 'subscriptions.stripe_status' selection.
I found a workaround, re-adding select('sites.*', 'subscriptions.stripe_status') after the withAnyTags() works.

Closing old issues.