KhalilurRehman's avatar

Pagination not working in laravel 5.3 with for loop iteration. Pagination object is being hide when i use for loop or foreach loop

Pagination is working without use of for loop, when want to do structuring with data using for or foreach loop, pagination is being hide from response. here is my code. Pagination object is being hide when i use for loop or foreach loop.

$calls = DB::table('call') ->where('call.company_id', '=', $company_id) ->orderBy('call.created_at', 'ASC') ->paginate($perPage, array( $columns ), 'current_page', $currentPage);

        $lead = new Lead();
        $i = 0;
        for($i=0; $i < count($calls); ++$i){

            $callData = ""; 
            $callData['id'] = $calls[$i]->id;
            $callData['date'] = $calls[$i]->date;
            $callData['duration'] = $calls[$i]->duration;
            $callData['user_id'] = $calls[$i]->user_id;
            $callData['contact_number'] = $calls[$i]->contact_number;
            $callData['from_time'] = $calls[$i]->from_time;
            $callData['call_type'] = $calls[$i]->call_type;
            $callData['created_by'] = $calls[$i]->created_by;
            $callData['created_at'] = $calls[$i]->created_at;
            $callData['updated_at'] = $calls[$i]->updated_at;

             $callsData[] = $callData;
            }

            return $callsData;
0 likes
3 replies
SaeedPrez's avatar

@KhalilurRehman of course it's being left out, your code is leaving it out...

What I don't understand is what are you trying to do accomplish with that loop? You're moving some properties from $calls and saving them as an array in $callsData

KhalilurRehman's avatar

Actually during foreach i have to add another user object based on phone number matched like following code.

$lead = new Lead(); foreach ($calls as $call) { // echo json_encode($calls);die; $callData = ""; $callData['id'] = $call->id; $callData['date'] = $call->date; $callData['duration'] = $call->duration; $callData['user_id'] = $call->user_id; $callData['contact_number'] = $call->contact_number; $callData['from_time'] = $call->from_time; $callData['call_type'] = $call->call_type; $callData['created_by'] = $call->created_by; $callData['created_at'] = $call->created_at; $callData['updated_at'] = $call->updated_at; $leads = $lead->getByPhoneNumber( $call->contact_number ); //Add lead data. if( $leads ){ $callData = array_add($callData, 'Lead', $leads); } $callsData[] = $callData; } return $callsData;

Thats why i need to use foreach or for loop to structure my data for proper and required response.

SaeedPrez's avatar

@KhalilurRehman I can't really make sense of that unformatted wall of text, but what is important to understand is that you're taking objects and converting them into an array and at the same time you're stripping away all the extra properties and methods that is required for pagination to work.

For demonstration, try this code and see the difference in the output..

    // Your for loop

    // Add these two line
    dump($calls);
    dump($callsData);

    return $callsData;

Please or to participate in this conversation.