Back

unable to find links in laravel for pagination


Kunal Singh posted 4 weeks ago

i get a json response like this note i am using GuzzleHttp to call my API

Front End Controller

  $response =  $response->getBody()->getContents();
   $output = (json_decode($response));
   dd(output);

JSON Response

{#232 ▼
  +"current_page": 1
  +"data": array:2 [▼
    0 => {#230 ▼
      +"id": 1
      +"test_col": "Test one"
    }
    1 => {#237 ▼
      +"id": 3
      +"test_col": "Test three"
    }
  ]
  +"first_page_url": "http://api/api/test?page=1"
  +"from": 1
  +"last_page": 8
  +"last_page_url": "http://api/api/test?page=8"
  +"next_page_url": "http://api/api/test?page=2"
  +"path": "http://api/api/test"
  +"per_page": 2
  +"prev_page_url": null
  +"to": 2
  +"total": 15
}

Front-End View

and now when i go to my front end and do this

 {{$outputs->links()}}

to get the pagination links it shows me an error

ErrorException (E_ERROR) Call to undefined method stdClass::links()

and on my API side i am doing this to get my above json API

      $results = DB::table('test_table')->paginate(2);
        return ($results);
Kunal Singh replied 2 weeks ago Solution

so i was able to fix this my doing this

 $pagination = new LengthAwarePaginator($outputs->data, $outputs->total, $outputs->per_page,
            $outputs->current_page, ['path' => '/test']);
 return view('test.index')->with('outputs',$outputs)->with('pagination',$pagination);

  {{($pagination->links())}} {{--Pagination Links--}}

Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us