Laravel 分页链接()不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48284599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:15:22  来源:igfitidea点击:

Laravel Pagination links() not working

phplaravelpagination

提问by Lukas J

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

我以这种格式在我的刀片页面上添加了添加的 for

@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

然后我{{ $activities->links() }}在页面上添加。无论何时呈现视图,links() 都不会显示页面的链接。我是不是遗漏了什么,或者我做错了什么

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

PS:我也试过{{$activities->render()}},也没用。

回答by Bhoomi Patel

Try this code in blade file:

在刀片文件中试试这个代码:

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

instead of

代替

{{$activities->render()}}

回答by Alexey Mezenin

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

在 Laravel 5.5 中,如果Paginator对象只有一页,将不会显示分页链接。

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:

1.运行此命令将分页视图复制到resources/views/vendor/pagination

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

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

2.编辑resources/views/vendor/pagination/default.blade.php.

Change the very first line:

更改第一行:

@if ($paginator->hasPages())

To:

到:

@if ($paginator->count > 0)

回答by balamurugan

Add the following line after "@endforeach" statement

在“@endforeach”语句后添加以下行

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

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

如果使用 bulma css 而不是 bootstrap 4,请自定义分页视图。

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

将以下“ bulma.blade.php”文件保存在路径“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

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

在“@endforeach”语句后添加以下行

{{ $activities->links('vendor.pagination.bulma') }}

回答by vaishnu devi

    $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***/

回答by urvisha

controller:   $users = DB::table('users')->Paginate(10);
 view file  : {{$users->links('vendor.pagination.default')}}

run the command:

运行命令:

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