8 months ago

Laravel get unique views

Posted 8 months ago by Dronax

I have this code on controller:

$shops = Shop::withCount(['views' => function ($query) {

I use this package: https://github.com/cyrildewit/eloquent-viewable In table views I have column visitor. Why distinct not working?

I get this query on result:

select `shops`.*, (select distinct count(*) from `views` where `shops`.`id` = `views`.`viewable_id` and `views`.`viewable_type` = 'App\Shop') as `views_count` from `shops` where `user_id` = 1 order by `id` asc limit 10 offset 0

Why distinct is not working?

I need get: count(distinct visitor), but not distinct count(*)

Please sign in or create an account to participate in this conversation.