5

After doing this in my controller

   $activities = Package::where('attraction',1)
              ->where('hotel', 0)
              ->where('flight', 0)
              ->paginate(2);
          return view('frontend.activities',compact('activities'));

I added the added the for on my blade page in this fomrmat

@foreach($activities as $activity)
                    <div class="col-md-3 page-content-1">
                        <div class="thumb">
                            <header class="thumb-header">
                                <a class="hover-img" href="{{url('/flight-details')}}">
                                    <img src="img/new_york_at_an_angle_800x600.jpg" alt="Image Alternative text" title="new york at an angle" />
                                    <h5 class="hover-title-center">Book Now</h5>
                                </a>
                            </header>
                            <div class="thumb-caption">
                                <ul class="icon-group text-tiny text-color">
                                    <li><i class="fa fa-star"></i>
                                    </li>
                                    <li><i class="fa fa-star"></i>
                                    </li>
                                    <li><i class="fa fa-star"></i>
                                    </li>
                                    <li><i class="fa fa-star"></i>
                                    </li>
                                    <li><i class="fa fa-star"></i>
                                    </li>
                                </ul>
                                <h5 class="thumb-title"><a class="text-darken" href="{{url('/flight-details')}}">Manhattan Skyline</a></h5>
                                <p class="mb0"><small><i class="fa fa-map-marker"></i> Queens (LaGuardia Airport (LGA))</small>
                                </p>
                                <p class="mb0 text-darken"><span class="text-lg lh1em text-color"><small >from</small> Free</span>
                                </p>
                            </div>
                        </div>
                    </div>
                    @endforeach

then i added {{ $activities->links() }} on the page. Whenever present the view, the links() does not display the links for the pages. Am i missing something or is there something that i am doing wrong

P.S : I have also tried {{$activities->render()}} , it did not work too.

Lukas J
  • 53
  • 1
  • 1
  • 4

9 Answers9

19

Add the following line after "@endforeach" statement

{{ $activities->links('pagination::bootstrap-4') }}

If bulma css is used instead of bootstrap 4, customize the pagination view.

Save the following "bulma.blade.php" file in path "resources/views/vendor/pagination"

if the folder vendor is not present in views folder, just create it or run the following command

php artisan vendor:publish --tag=laravel-pagination

bulma.blade.php

@if ($paginator->hasPages())
    <nav class="pagination is-centered" role="navigation" aria-label="pagination">
        {{-- Previous Page Link --}}
        @if ($paginator->onFirstPage())
            <a class="pagination-previous"  disabled>Previous</a>
        @else
            <a class="pagination-previous" href="{{ $paginator->previousPageUrl() }}" rel="prev">Previous</a>
        @endif

        <ul class="pagination-list">
            {{-- Pagination Elements --}}
            @foreach ($elements as $element)
                {{-- "Three Dots" Separator --}}
                @if (is_string($element))
                    <li><span class="pagination-ellipsis">{{ $element }}</span></li>
                @endif

                {{-- Array Of Links --}}
                @if (is_array($element))
                    @foreach ($element as $page => $url)
                        @if ($page == $paginator->currentPage())
                            <li><span class="pagination-link is-current">{{ $page }}</span></li>
                        @else
                            <li><a href="{{ $url }}" class="pagination-link">{{ $page }}</a></li>
                        @endif
                    @endforeach
                @endif
            @endforeach
        </ul>

    {{-- Next Page Link --}}
    @if ($paginator->hasMorePages())
        <a class="pagination-next" href="{{ $paginator->nextPageUrl() }}" rel="next">Next page</a>
    @else
        <a class="pagination-next" disabled>Next page</a>
    @endif
@endif

Add the following line after "@endforeach" statement

{{ $activities->links('vendor.pagination.bulma') }}
balamurugan
  • 364
  • 2
  • 5
4

In Laravel 5.5 if Paginator object has just one page, pagination links will not be displayed.

If you want to display these links anyway, you need to customize the pagination view.

1. Run this command to copy pagination views to resources/views/vendor/pagination:

php artisan vendor:publish --tag=laravel-pagination

2. Edit the resources/views/vendor/pagination/default.blade.php.

Change the very first line:

@if ($paginator->hasPages())

To:

@if ($paginator->count > 0)
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
3

Try this code in blade file:

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

instead of

{{$activities->render()}}
Bhoomi Patel
  • 777
  • 10
  • 32
1

According to this trade https://stackoverflow.com/a/63852468/10938614 Laravel 8 has new ways and options for adding how pagination can be displayed.

Add pagination in AppServiceProvider class as below

use Illuminate\Pagination\Paginator;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Paginator::useBootstrap();
}
Chetam Okafor
  • 493
  • 4
  • 7
0
    $items = $user_1->all();

    // Store records in an array
    $records = [];

    $i = 0;
    foreach($items as $item)
    {
        $records[$i][0] = $item->user_name;
        $records[$i][1] = $item->rech_mobile;
        $i++;
    }


    /****above code was replaced by the following code***/
    $items = $user_1->all()->toArray();
       /***---toArray() function used to convert object to array***/
  • 1
    Please add some explanation to your code. Additionally your code contains an syntax error. The last line should be a comment. Reread your answer before you post it. – common sense Dec 12 '18 at 09:55
0
controller:   $users = DB::table('users')->Paginate(10);
 view file  : {{$users->links('vendor.pagination.default')}}

run the command:

php artisan vendor:publish --tag=laravel-pagination
tryingToLearn
  • 10,691
  • 12
  • 80
  • 114
0

One or some of your browser extensions maybe conflict with Tailwind Pagination rendering part. Try to turn off extensions which inspect background page.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 21 '22 at 08:23
0

In my case with nginx config.

I have fixed it by changing the config of nginx server. Because laravel can not get parameter 'page' thefore the links() would not be working.

in default.conf of nginx :

From:

location / {
        try_files $uri $uri/ /index.php?$query_string;
}

TO

location / {
        try_files $uri $uri/ /index.php?$args;
}

I hope someone can remove this error and go ahead with laravel.

vantinh
  • 1
  • 1
0

you must have enough data to display in more that 1 page, so you must have more than 10 rows of data for the links to show, when you display 10 rows of data per page.