Pagination is not working in twilio call logs laravel

六月ゝ 毕业季﹏ 提交于 2021-02-11 15:48:07

问题


<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\ApiController;
use App\Model\User;
use Illuminate\Support\Facades\Validator;
use Auth;
use Twilio\Rest\Client;
use Twilio\TwiML\VoiceResponse;

class TwilioController extends ApiController
{
    public function __construct(){
        $sid = env('TWILIO_SID');
        $token = env('TWILIO_TOKEN');
        $this->twilio = new Client($sid, $token);
        if (request('lang'))
            \App::setLocale(request('lang'));
    }

    public function callLogs(Request $request){
        try{
            $twilioNumber = Auth::user()->twilio_number;
            $calls = $this->twilio->calls
                ->read([], 20);
                $data = [];
                $i = 0;
            foreach($calls as $call){
                $data[$i]['from'] = $call->from;
                $i++;
            }
            $responseData = array('status'=>true, 'message'=>'Data has been returned successfully!', 'data'=>$data);
        } catch (\Exception $e) {
            $responseData = array('status'=>false, 'message'=>$e->getMessage(), 'data'=>[]);
        }
        $res = json_encode($responseData);
        print $res;

    }

Pagination is not working when i get call history in twilio using laravel rest api. when i use page parameter with this then pagination is not working it give me save output as given me 1st page. Postman parameters - Page:2

Thanks


回答1:


I think Twilio's page() function will help you. Like read(), it accepts an array of options to narrow your search along with a pageSize parameter defaulting to 50 calls per page. (I believe the maximum value is 1000.)

Here is an example.

//Get all call logs. 20 per page
$calls = $twilio->calls->page( [], 20 );

$data = [];

foreach($calls as $call){
    $data[] = $call->sid;
}

//You can access the previous and next page urls using the resource functions in the calls object return from twilio.

return response()->json(["prev" => $calls->getPreviousPageUrl(), "next" => $calls->getNextPageUrl(), "calls" => $data]);


来源:https://stackoverflow.com/questions/61485160/pagination-is-not-working-in-twilio-call-logs-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!