0

here is my code:

$contents = Tag::where('user_id', $loggedUser)
                        ->distinct('tag')
                        ->paginate(100)->toArray();

As result i get all tags based on user id but with repetetion. can someone tell why distinct is not working?

compulsive coder
  • 164
  • 1
  • 10
  • 1
    Change it with groupBy `Tag::where('user_id', $loggedUser)->groupBy('tag')->paginate(100)->toArray();` – STA Nov 18 '20 at 16:21
  • 1
    Does this answer your question? [How to get distinct values for non-key column fields in Laravel?](https://stackoverflow.com/questions/25228823/how-to-get-distinct-values-for-non-key-column-fields-in-laravel) – miken32 Nov 18 '20 at 16:24
  • @sta : Getting error. ``` SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'database_name.tags.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select count(*) as aggregate from (select * from `tags` where `user_id` = 235 group by `tag`) as `aggregate_table`)``` – compulsive coder Nov 18 '20 at 16:30
  • @miken32 nope. it dont have where clause and paginations – compulsive coder Nov 18 '20 at 16:31

1 Answers1

2

I guess u need to get all tags of a user based on his $user_id. If you need only tags as an array, Try this. add select() and give it a try.

$contents = Tag::where('user_id', $loggedUser)
                        ->select('tag')
                        ->distinct('tag')
                        ->paginate(100)->toArray();

Let me know if u have any more issues.

Rishad
  • 381
  • 2
  • 11