vendredi 25 septembre 2015

Larval Reading from API with Limits and Displaying onto View with Pagination

Hi I am a little confused on how can I read from an api with page item limited display and then paginate them onto view with the pagination, such that when I click on the pagination link on the view to view next page, the display will be able to show based on the data from the api?

Here is the scenario of what I am doing, I have an api link that is like this: http://localhost:8888/api/v1/categories, which will display a json list of categories limited to 5 per page like following. And the next page will be http://localhost:8888/api/v1/categories?page=2.

    {
  "data": [
    {
      "id": 1,
      "name": "Arts & Entertainment",
      "status": true,
      "created_at": "2015-09-20 16:19:15",
      "updated_at": "2015-09-25 12:58:52"
    },
    {
      "id": 2,
      "name": "Travel",
      "status": true,
      "created_at": "2015-09-20 16:19:23",
      "updated_at": "2015-09-25 12:59:02"
    },
    {
      "id": 3,
      "name": "Child Development",
      "status": true,
      "created_at": "2015-09-20 16:19:28",
      "updated_at": "2015-09-25 12:59:07"
    },
    {
      "id": 4,
      "name": "Computers / Internet",
      "status": true,
      "created_at": "2015-09-20 16:19:44",
      "updated_at": "2015-09-25 12:59:27"
    }
  ],
  "paginator": {
    "total_count": 15,
    "total_pages": 3,
    "current_page": 1,
    "limit": 5
  }
}

I am using Laravel 5.1, and GuzzleHttp to read this data like so:

public function index()
    {
        $client = new Client(['base_uri' => 'http://localhost:8888/api/v1/']);
        $response = $client->get('categories')->getBody();
        $content = json_decode($response->getContents());

        // how to continue with the pagination and display onto the view with the pagination function?
        return view('categories', ['content' => $content->data]);
    }

Views:

<div class="container">
    <ul>
    @foreach($content as $value)
        <li>{{ $value->name }}</li>
    @endforeach
    </ul>
</div>

Thanks in advance for taking time to look through.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire