3

I have implemented a laravel pagination in one of my project where pagination is working like a charm.

I have a requirement to display a pagination links on top of the table as well as the bottom of the table. Like this

{!! $entries->render() !!}
<table class="table table-responsive" id="entries-table">
    <thead>
        <tr>
                <th>ID</th>
                <th>Advisor name</th>
        </tr>
    </thead>
    <tbody>
    @forelse($entries as $entries)
        <tr>
            <td> {{ $entries->id }} </td>
            <td> {{ $entries->name }} </td>
        </tr>
    @empty
    <tr>
        <td>
            <p>No record found.</p>
        </td>
    </tr>
    @endforelse
    </tbody>
</table>
{!! $entries->render() !!}

When I try to use {!! $entries->render() !!} one more time in the bottom of the table or anywhere in a same page, it throws me the below error.

ErrorException (E_ERROR) Call to undefined method App\Models\Entries::render()

here is my Controller code

public function index(Request $request)
    {            
        $entries = Entries::orderBy('id', 'DESC')->paginate(15);

        return view('entries.index')
            ->with('entries', $entries);
    }

here I am dumping the varialbe $entries in controller using dd($entries) and this is what I am getting.

LengthAwarePaginator {#425 ▼
  #total: 215
  #lastPage: 15
  #items: Collection {#436 ▼
    #items: array:15 [▼
      0 => entries {#437 ▶}
      1 => entries {#438 ▶}
      2 => entries {#439 ▶}
      3 => entries {#440 ▶}
      4 => entries {#441 ▶}
      5 => entries {#442 ▶}
      6 => entries {#443 ▶}
      7 => entries {#444 ▶}
      8 => entries {#445 ▶}
      9 => entries {#446 ▶}
      10 => entries {#447 ▶}
      11 => entries {#448 ▶}
      12 => entries {#449 ▶}
      13 => entries {#450 ▶}
      14 => entries {#451 ▶}
    ]
  }
  #perPage: 15
  #currentPage: 1
  #path: "https://samplesite.com/entries/11"
  #query: []
  #fragment: null
  #pageName: "page"
  +onEachSide: 3
  #options: array:2 [▼
    "path" => "https://samplesite.com/entries/11"
    "pageName" => "page"
  ]
}

Check this Video i am sharing with you for better idea

Punit Gajjar
  • 4,937
  • 7
  • 35
  • 70
  • What if you tried setting the `$entries->render()` to a variable like `@php $render = $entries->render(); @endphp` and use `$render` on the places where you use `$entries->render();` – Refilon Feb 12 '20 at 08:24
  • @Refilon , I have just tried that, Not working..... I feel laravel it self not allowing to use it multiple times.. – Punit Gajjar Feb 12 '20 at 08:57

4 Answers4

1

You should not give the same name in the variable on the foreach, it could result in a conflict between.errors that might occur because abigu of a variable from the eloquent results or from the results in the loop.

try refactor your code like this

@forelse($entries as $entrie)
    <tr>
        <td> {{ $entrie->id }} </td>
        <td> {{ $entrie->name }} </td>
    </tr>
@empty
<tr>
    <td>
        <p>No record found.</p>
    </td>
</tr>
@endforelse
Qonvex620
  • 3,819
  • 1
  • 8
  • 15
good112233
  • 144
  • 1
  • 10
1

Make sure you'r returning the exact collection in your blade.

$users= User::paginate(15);

return view('entries', compact('users'));

and you can use already this line in your blade.

{!! $entries->links() !!}

Or

{!! $users->render() !!}

and also in your for each, change it to this

@forelse($entries as $entry)
     <tr>
         <td> {{ $entry->id }} </td>
         <td> {{ $entry->name }} </td>
     </tr>
@empty

Try to change your controller to this.

  public function index(Request $request)
    {            
            $entries = entries::where('form_id', '=', $id)->orderBy('id', 'DESC')->paginate(15);

            return view('entries.index')
                ->with('entries', $entries);
    }
Qonvex620
  • 3,819
  • 1
  • 8
  • 15
1

You are overriding $entries collection with collection item in @forelse.

@forelse($entries as $entries)
    <tr>
        <td> {{ $entries->id }} </td>
        <td> {{ $entries->name }} </td>
    </tr>
@empty

Change $entries to $entry:

@forelse($entries as $entry)
    <tr>
        <td> {{ $entry->id }} </td>
        <td> {{ $entry->name }} </td>
    </tr>
@empty
IndianCoding
  • 2,602
  • 1
  • 6
  • 12
0

Have you tried {{ $entries->links() }}?

update

in your controller can you try this

    $users = User::paginate(1);
    dd($users->links(), $users->links());

do you have the same error?

Andy Song
  • 4,404
  • 1
  • 11
  • 29
  • I tried. Same Error :-( `Call to undefined method App\Models\Entries::links()` – Punit Gajjar Feb 12 '20 at 07:33
  • Bro, On Controller side it display , and You can dump same varialbe n number of times in `dd()` Thats not the solution, while I use in my view file it doesn't work thats the problem.. – Punit Gajjar Feb 12 '20 at 07:49