1

I am using Laravel Analytics and calling Analytics data, I am trying to paginate the data but cannot:

Controller:

public function analytics()
{   
    $analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(1))->paginate(10);
    $no = 0;
    return view('admin.analytics',compact(['analyticsData', 'TotalVisitors', 'no']));
}

Views:

<table class="table table-hover">
    <thead>
      <tr>
        <th>S.No.</th>
        <th scope="col">Date</th>
        <th scope="col">Visitors</th>
        <th scope="col">PageTitle</th>
        <th scope="col">PageViews</th>
      </tr>
    </thead>
      <tbody>
        @foreach ($analyticsData->sortBy('pageViews')->reverse() as $data)
        <tr>
          <td>{{ ++$no }}</td>
          <td>{{ $data['date'] }}</td>
          <td>{{ $data['visitors'] }}</td>
          <td>{{ $data['pageTitle'] }}</td>
          <td>{{ $data['pageViews'] }}</td>
        </tr>
        @endforeach
      </tbody>
</table>
 @if( $analyticsData->count() != 0 )    
 {{ $analyticsData->links('pagination::bootstrap-4') }}
 @endif

But I get an error: Method Illuminate\Support\Collection::paginate does not exist.

dd($analyticsData);

gives me:

enter image description here

Anonymous Girl
  • 582
  • 8
  • 22
  • `fetchVisitorsAndPageViews(Period::days(1)->paginate(10)` ===>>> I hope that's a typo, but let's say it is. Then you can't paginate a collection, so ... cut it up in pieces. Whenever you have a piece of a collection you have a ... `chunk()` try that – UnderDog Dec 25 '22 at 05:24
  • @UnderDog That was a typo, I corrected and also tried `->chunk(20)` but that does not help instead it creates issue in View as `Undefined array key "date"` – Anonymous Girl Dec 25 '22 at 05:29
  • 1
    Keep trying to update your original question. Reading updates from these tiny comments give me headaches. So ... you have an array in your view. You did send that array from your controller. An array key is missing. Todo: go to the controller and `dd($analyticsData);`, please paste the results in your original question. Then ... go to your function. Look at the select that you are doing (copy/paste in original question). Did you select the column 'date'? – UnderDog Dec 25 '22 at 06:48
  • 1
    @UnderDog Added the screenshot of the output – Anonymous Girl Dec 25 '22 at 06:54
  • In your blade view, within the @fireach loop, you have the opportunity to add @dd() or @dump() where you can debug the variables. In your case you need to debug the $data variable – UnderDog Dec 25 '22 at 07:01

0 Answers0