uccdev
7 months ago
767
8
Laravel

Is it possible to paginate two different tables on the same view?

Posted 7 months ago by uccdev

So I have a laravel project that fetches data from not one, but two API endpoints. API endpoints like Canvas, so I cannot use any DB::table queries or other tricks. It gets the data from a Courses endpoint as well as a Users endpoint. I've been requested to fit the data of both tables on the same view.

Displaying all the data is fine, I'm able to collect the data from both tables in my controller and parse it to the view:

return view('index')
  ->with("courseData", $courseData) //course Data
  ->with("cPages", $cPages) //course pages
  ->with("cRels", $cRels) //course references 
  ->with("cPageNums", $pageNums) //course page numbers
  ->with("userData", $userData) //user data
  ->with("uPages", $uPages) //user pages
  ->with("uRels", $uRels) //user references
  ->with("uPageNums", $uPageNums); //user page numbers

I'm also able to successfully paginate the Courses table.

if (isset($cPageNums)) {
      ?><a href="<?php echo $cPageNums[0]; ?>" rel="<?php echo $cRels[0]; ?>"><?php echo $cRels[0]; ?></a>
      <?php
        if ($cPages[1] != null ) {
          ?> <a href="<?php echo $cPageNums[1]; ?>" rel="<?php echo $cRels[1]; ?>"><?php echo $cRels[1]; ?></a>
        <?php
      } else { echo "Error"; }
        if ($cPages[2] != null) {
        ?>  <a href="<?php echo $cPageNums[2]; ?>" rel="<?php echo $cRels[2]; ?>"><?php echo $cRels[2]; ?></a>
        <?php
        }
        if ($cPages[3] != null) {
          ?> <a href="<?php echo $cPageNums[3]; ?>" rel="<?php echo $cRels[3]; ?>"><?php echo $cRels[3]; ?></a>
  <?php }
    } else {
      echo "No course pageNums set!";
    }

For comparison's sake, my User pagination code, which is only different in that it uses different variables:

if (isset($uPageNums)) {
        ?><a href="<?php echo $uPageNums[0]; ?>" rel="<?php echo $uRels[0]; ?>"><?php echo $uRels[0]; ?></a>
        <?php
      if ($uPages[1] != null ) {
        ?> <a href="<?php echo $uPageNums[1]; ?>" rel="<?php echo $uRels[1]; ?>"><?php echo $uRels[1]; ?></a>
      <?php
    } else { echo "Error"; }
      if ($uPages[2] != null) {
      ?>  <a href="<?php echo $uPageNums[2]; ?>" rel="<?php echo $uRels[2]; ?>"><?php echo $uRels[2]; ?></a>
      <?php
      }
      if ($uPages[3] != null) {
        ?> <a href="<?php echo $uPageNums[3]; ?>" rel="<?php echo $uRels[3]; ?>"><?php echo $uRels[3]; ?></a>
<?php }
  } else {
    echo "No user pageNums set!";
  }
  ?>

So what happens when I click the buttons these create in my view? Well if they're the Course buttons, they work perfectly fine. I move between all the pages I need to.

When I click the user ones? I move through the Course table instead. It's as if the page only recognises the one set, not the other. So I suppose I've two big questions.

  1. Is what I'm looking to do possible? Paginate between two tables on the same webpage? I'm aware it might be easier to simply have a separate view for each, but management would prefer this way.

  2. If it is possible, what can I do to accomplish that? Or what is the mistake I'm presently making?

Any help on this matter would be greatly appreciated.

EDIT: For further clarity, here is how I sort pagination. Canvas naturally paginates its data, so I try to retrieve its links directly from that. Then after extraction, I pass the data containing those onto the view.

    $explData = explode("\n",$resp); //Need the page links in order to move from data to data, since Canvas keeps all its data paginatable.

    $courseLinks = self::getLinks($explData); //gets links from data
    if (is_null($links)) {
      echo "exec'd data did not contain any links";
      return null;
    }
    $cPageLinks = explode(",", $links); //the actual strings in full. Array form.
    $cPages = array_fill(0, 4, null); //the page URL links
    $cRels = array_fill(0, 4, null); //references (i.e "current", "next", "prev", "last")
    $cPageNums = array_fill(0, 4, null); //amount of pages
    $i = 0;
    //loop to populate the above
    foreach($cPageLinks as $p) {  //get the important page details and store them in these arrays
      $cPages[$i] = AppHelper::instance()->get_string_between($cPageLinks[$i], "<", ">"); //helper method, returns raw string.
      $cRels[$i] = AppHelper::instance()->get_string_between($cPageLinks[$i], 'rel="', '"');
      $cPageNums[$i] = AppHelper::instance()->get_string_between($cPageLinks[$i], "?page=", "&");
      $i++;
}

This process is mirrored for getting the User endpoint's pages.

Please sign in or create an account to participate in this conversation.