leonan's avatar

Pagination Issue

Hello,

In an attempt to display the most recent activity updates by some users I am querying the database to grab only 20 updates and to paginate them at 5 per page. But the query below grabs and displays all logged activities (35) at 5 per page, which I only want 20.


Using ->paginate

$paginator = DB::table('activity_log')
                ->leftJoin('assigned_agents', function($leftJoin) use ($userLoggedin)
                {
                    $leftJoin->on('activity_log.user_id', '=', 'assigned_agents.user_id');
                })
                ->leftJoin('users', function($leftJoin)
                {
                    $leftJoin->on('activity_log.user_id', '=', 'users.id');
                })
                 ->where('assigned_agents.agent_id', '=' ,$userLoggedin->id)
                ->select('activity_log.user_id', 'activity_log.content_type', 'activity_log.content_id', 'activity_log.details', 'activity_log.action', 'activity_log.created_at', 'assigned_agents.agent_id', 'users.first_name')
                ->orderBy('activity_log.created_at', 'DESC')
                ->take(20)
                ->paginate(5);

So I attempted to pass the results to Paginator::make, the parameters for the Paginator:make (totalitems = 20, perpage = 5) are ignored.

My view has a foreach to iterate through the results but it displays 20 records per page even though it's set to 5 per page, and on top of that each page displays the same 20 records.

When I change ->take(20) to ->take(5), it displays 5 per page and they are the same 5 records.


Using Paginator::make

            $activity = DB::table('activity_log')
                ->leftJoin('assigned_agents', function($leftJoin) use ($userLoggedin)
                {
                    $leftJoin->on('activity_log.user_id', '=', 'assigned_agents.user_id');
                })
                ->leftJoin('users', function($leftJoin)
                {
                    $leftJoin->on('activity_log.user_id', '=', 'users.id');
                })
                 ->where('assigned_agents.agent_id', '=' ,$userLoggedin->id)
                ->select('activity_log.user_id', 'activity_log.content_type', 'activity_log.content_id', 'activity_log.details', 'activity_log.action', 'activity_log.created_at', 'assigned_agents.agent_id', 'users.first_name')
                ->orderBy('activity_log.created_at', 'DESC')
                ->take(20)
                ->get();

        $paginator = Paginator::make($activity, 20, 5);

        return View::make('inhouse/dashboard', compact('userLoggedin', 'paginator'));

VIEW

        <ul class="recentActivityUl">

            @forelse($paginator as $recent)

                <li class="animated fadeIn">

                     <a title="" href="">

                         <p class="activityDetails"> 

                                     User {{ $recent->first_name}}{{ $recent->details }} {{ $recent->agent_id }}<span class="activityDate">{{{ Carbon::parse($recent->created_at)->diffForHumans() }}} </span>

                         </p>

                     </a>
                     <div style="clear: both;"></div>
                </li>

            @empty

                <li>No activity</li>

            @endforelse

            {{ $paginator->links() }}

        </ul> 

I'm not sure if it's my joins that are messing up the results or the logic is off, any guidance is appreciated.

0 likes
0 replies

Please or to participate in this conversation.