Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ldslaracasts's avatar

Further refine a query once it hits blade

I am using moloquent (mongodb eloquent) to query a local DB to return a collection of tweets. Once I have the tweets from that query, I need the option to group them by account. I have the page set up to display each account that was present in the result and how many tweets they had. The problem I am running in to is generating the links so that when the user clicks on that account name, they will be moved to a new view that has only the tweets from that account.

0 likes
2 replies
topvillas's avatar

If you're displaying the account then you must have the id.

It's not very clear what problem you're having. Show us your blade template.

ldslaracasts's avatar

Here is what I have up to this point.

@extends('layouts.results')
@section('content')

    <link rel="stylesheet" href="{{asset('css/tweet_card.css')}}">
    <link rel="stylesheet" href="{{asset('css/confirm_account.css')}}">

    <div class="container">

        {{--Display error if no results found--}}
        @if(count($fullList) == 0)
            <div class="banner">
                <h1>No results found. Please try another search</h1>
                <a href="/twitter">
                    <input type="button" class="btn btn-primary" name="newSearch" value="New Search"/>
                </a>
            </div>
        @else
            <div class="banner">

                {{--Count all results--}}
                <h1>{{count($fullList)}} results found</h1>

                <a href="/twitter">
                    <input type="button" class="btn btn-primary" name="newSearch" value="New Search"/>
                </a>
            </div>

            {{--Get screen name for each account--}}
            @php($accounts = array())
            @php($counter = 1)
            @foreach($fullList as $tweet)
                @php(array_push($accounts, $tweet['user']['screen_name']))
            @endforeach

            {{--Get tweet count for each account--}}
            @php($accounts = array_count_values($accounts))

            <div class="panel panel-default">
                <div class="panel-heading" role="tab" id="headingOne">
                    <h4 class="panel-title">
                        <a role="button" data-toggle="collapse" href="#collapseOne" aria-expanded="true"
                           aria-controls="collapseOne" class="trigger collapsed">
                            Accounts - Alphabetical
                        </a>
                    </h4>
                </div>
                <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">

                    {{--Use JS to order accounts by name--}}
                    <div id="byname" class="panel-body">
                    </div>
                </div>
            </div>
            <div class="panel panel-default">
                <div class="panel-heading" role="tab" id="headingTwo">
                    <h4 class="panel-title">
                        <a role="button" data-toggle="collapse" href="#collapseTwo" aria-expanded="true"
                           aria-controls="collapseTwo" class="trigger collapsed">
                            Accounts - Tweet Count
                        </a>
                    </h4>
                </div>
                <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">

                    {{--Use JS to order accounts by tweet count--}}
                    <div id="bytweets" class="panel-body">
                    </div>
                </div>
            </div>
    </div>
    <hr/>
    @endif

    <div class="flex-grid">

        {{--Start of Tweet--}}
        @foreach($tweets as $tweet)
            <div class="cardcontainer">
                <header>

                    {{--Isolate any videos in tweet--}}
                    @if(!empty($tweet['extended_entities']['media']))
                        @foreach($tweet['extended_entities']['media'] as $media)
                            @if(!strpos($media['media_url_https'], 'thumb'))
                                <a href="{{$media['media_url_https']}}" target="_blank">
                                    <img class="tweet-img" src="{{$media['media_url_https']}}">
                                </a>
                            @endif

                            @if(!empty($media['video_info']))
                                @foreach($media['video_info']['variants'] as $video)
                                    @if(strpos($video['url'], 'mp4') && $videoCount == 0)
                                        <a href="{{$video['url']}}" target="_blank">
                                            <img class="tweet-img"
                                                 src="{{$media['media_url_https']}}">
                                        </a>
                                        {{--Track if video is present for search purposes--}}
                                        @php($videoCount = 1)
                                    @endif
                                @endforeach
                                @php($videoCount = 0)
                            @endif
                        @endforeach
                        <hr/>
                    @endif
                </header>

                {{--Populate tweet content--}}
                <div class="data">
                    <div class="item-heading">
                        <a href="https://twitter.com/{{$tweet['user']['screen_name']}}" target="_">
                            <img src="{{$tweet['user.profile_image_url_https']}}" alt="avatar"
                                 class="avatar">
                            <h3><?php echo '@'; ?>{{$tweet['user.screen_name']}}</h3></a>
                        @php($date = date('F jS, Y',$tweet['created_at.sec']))
                    </div>
                    <p>
                    {{$date}}
                    <hr/>
                    @if(!empty($tweet['full_text']))
                    {{$tweet['full_text']}}
                    @endif
                    </p>
                </div>
            </div>

        {{--End of tweet--}}
        @endforeach

    </div>

    @if(count($tweets) > 0)

        <div class="text-center">

            {{$tweets->render()}}

        </div>

        @endif
        </div>


        <script>

            //Clear list
            list = '';

            //Populate list with account names
            list ={!! json_encode($accounts) !!};

            //Sort accounts by tweet count
            accountlist = sortByValue(list);
            document.getElementById('bytweets').innerHTML += display(accountlist);

            //sort accounts Alphabetically
            tweetlist = sortByKey(list);
            document.getElementById('byname').innerHTML += display(tweetlist);


            function sortByValue(jsObj) {
                var sortedArray = [];
                for (var i in jsObj) {
                    sortedArray.push([i, jsObj[i]]);
                }
                return sortedArray.sort((function (a, b) {
                    return a[1] - b[1];
                })).reverse();
            }


            function sortByKey(jsObj) {
                var sortedArray = [];
                for (var i in jsObj) {
                    sortedArray.push([i, jsObj[i]]);
                }
                return sortedArray.sort(function (a, b) {
                    return a[0].toLowerCase().localeCompare(b[0].toLowerCase());
                });
            }


            //Display management
            function display(a) {
                var content = '';
                counter = 1;
                for (let i = 1; i <= a.length - 1; i++) {

                    if (counter == 1) {
                        content += '<div class="row">';

                    }

                    content += '<div class="col-xs-2">' + a[i][0] + ' (' + a[i][1] + ')</div>';
                    if (counter == 6) {
                        content += '</div>';

                        counter = 0;
                    }
                    counter++;
                }

                return content;
            }
        </script>
@endsection

Somewhere around this section

            {{--Get screen name for each account--}}
            @php($accounts = array())
            @php($counter = 1)
            @foreach($fullList as $tweet)
                @php(array_push($accounts, $tweet['user']['screen_name']))
            @endforeach 

I want to narrow down the query so that I can create a link to each specific accounts tweets so that this:

  content += '<div class="col-xs-2">' + a[i][0] + ' (' + a[i][1] + ')</div>';

would turn in to something like this:

  content += '<div class="col-xs-2"><a href="link-to-this-accounts-tweets">' + a[i][0] + ' (' + a[i][1] + ')</a></div>';

Please or to participate in this conversation.