0

I am pulling the list of post authors with highest likes from the database using the piece of code below. However, the output is such that if an author has more than one posts that also rank high, their name get pulled against those two posts. The desire output is to only pull their post with the highest likes and skip the second occurrence of their post that may also rank higher that other people post. Just so, no author get recognized twice. NOTE: There is no likes in the Post table; Likes has own table likeables , it uses belongs to relationship method. In summary, in the table shown below for example, I will like that the second occurence of David post with 7 'likes' be skipped. Thanks!

Serial name no of likes
1 John 11
2 Ravi 10
3 David 9
4 David 7
5 Paul 5
6 Azeez 1
@php
                $UserNot = [2,3];
                $PostNot = [];
                $verified = App\Models\User::query()->whereNotIn('id', $UserNot)->where('role', 'verified')->pluck('id')->toArray();
                $winners = App\Models\Post::select('user_id')->distinct()->whereNotIn('user_id', $UserNot)->whereIn('user_id', $verified)->wherePostLive(1)->withCount('likes')->orderBy('likes_count', 'desc')->get()->take(7);                
            @endphp
        <table class="table table-sm" id="caltbl">
            <tbody>
                @foreach($winners as $keys => $winner)
                    @php
                        $likesTotals = App\Models\Post::wherePostLive(1)->withCount('likes')->orderBy('likes_count', 'desc')->where('user_id', $winner->user->id)->get();
                        $uniqueUsers = App\Models\Post::select('user_id')->distinct()->wherePostLive(1)->withCount('likes')->where('user_id', $winner->user->id)->first();
                        $totalLikes = 0;
                        foreach($likesTotals as $key => $likesTotal) {
                            $totalLikes += $likesTotal->likes_count;
                        }
                    @endphp
                    <tr>
                        <td>{{$keys+1}}</td>
                        @if($totalLikes > 0)
                        <td class="sortnr" style="display: flex;">
                            <a style="display: contents;" class="author" href="{{ url('/profile/' . $winner->user->username) }}">            
                                <div>
                                    {{ str_limit($winner->user->name, 20) }}
                                </div>            
                                @isset($winner->user->role)
                                <div class="vbadge">
                                    <i class="icon-patch-check-fill verficon" style="vertical-align: inherit;" title="@lang('messages.new.verified')"></i>
                                </div>
                                @else
                                <div class="vbadge">
                                    <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="currentColor" class="bi bi-patch-minus" viewBox="0 0 16 16">
                                        <path fill-rule="evenodd" d="M5.5 8a.5.5 0 0 1 .5-.5h4a.5.5 0 0 1 0 1H6a.5.5 0 0 1-.5-.5z"/>
                                        <path d="m10.273 2.513-.921-.944.715-.698.622.637.89-.011a2.89 2.89 0 0 1 2.924 2.924l-.01.89.636.622a2.89 2.89 0 0 1 0 4.134l-.637.622.011.89a2.89 2.89 0 0 1-2.924 2.924l-.89-.01-.622.636a2.89 2.89 0 0 1-4.134 0l-.622-.637-.89.011a2.89 2.89 0 0 1-2.924-2.924l.01-.89-.636-.622a2.89 2.89 0 0 1 0-4.134l.637-.622-.011-.89a2.89 2.89 0 0 1 2.924-2.924l.89.01.622-.636a2.89 2.89 0 0 1 4.134 0l-.715.698a1.89 1.89 0 0 0-2.704 0l-.92.944-1.32-.016a1.89 1.89 0 0 0-1.911 1.912l.016 1.318-.944.921a1.89 1.89 0 0 0 0 2.704l.944.92-.016 1.32a1.89 1.89 0 0 0 1.912 1.911l1.318-.016.921.944a1.89 1.89 0 0 0 2.704 0l.92-.944 1.32.016a1.89 1.89 0 0 0 1.911-1.912l-.016-1.318.944-.921a1.89 1.89 0 0 0 0-2.704l-.944-.92.016-1.32a1.89 1.89 0 0 0-1.912-1.911l-1.318.016z"/>
                                    </svg>
                                </div>
                                @endisset                
                            </a>
                        </td>
                        <td>
                            <span style="background-color: #def8dd !important; color: #007e53 !important;font-weight: 400; padding: 0px 5px; border-radius: 20px; border: 1px solid #ccc;">{{$winner->likes_count}}</span>
                        </td>
                        @endif
                    </tr>
                @endforeach
            </tbody>
        </table>

myJourney
  • 33
  • 9
  • 1
    First, you shouldnt be doing the query in your view file. Second, you can do a group by and select `MAX` likes_count. Reference: https://stackoverflow.com/questions/12102200/get-records-with-max-value-for-each-group-of-grouped-sql-results – user3532758 Apr 14 '22 at 02:49

0 Answers0