Laravel,links()分页不支持GET方法。

huangapple go评论126阅读模式
英文:

Laravel, links() pagination The GET method is not supported

问题

When I process a post request for search, I get all result with pagination, when try to navigate to the second page http://localhost/project/admin/search?page=2

** The GET method is not supported for this route. Supported methods: POST.**

the links() function use GET to navigate in pages, how i can resolve ?

route :

Route::post('search',['as' => 'cat.search','uses' =>'CatController@search']);

function :

public function search(Request $request)
{
$search = $request->get('search');
if($search != ''){
$cat = Tombe::where('name','like', '%'.$search.'%')
->orwhere('description','like', '%'.$search.'%')
->paginate(15);
if(count($cat)>0){
return view('admin.cat.search', compact('cat'));
}else{
return back()->with('error','No results Found');
}
}else{
return back()->with('error','Enter Search Name');
}
}

英文:

When I process a post request for search, I get all result with pagination,
when try to navigate to the second page http://localhost/project/admin/search?page=2

** The GET method is not supported for this route. Supported methods: POST.**

the links() function use GET to navigate in pages, how i can resolve ?

route :

Route::post('search',['as' => 'cat.search','uses' =>'CatController@search']);

function :

public function search(Request $request)
    {
       
        $search = $request->get('search');
        if($search != ''){
            $cat =  Tombe::where('name','like', '%'.$search.'%')
            ->orwhere('description','like', '%'.$search.'%')
            ->paginate(15);
            if(count($cat)>0){
                return view('admin.cat.search', compact('cat'));
            }else{
                return back()->with('error','No results Found');
            }
        }else{
            return back()->with('error','Enter Search Name');
        }
    }

答案1

得分: 0

以下是翻译好的部分:

正如Tim Lewis所指出的,分页器默认使用GET请求。要使其使用POST请求,您需要为Paginator的links()方法提供一个自定义视图,将每个<a>标签替换为一个<form>

例如,可以在GitHub仓库中看到默认视图,以及一些其他使用Bootstrap或Tailwind的视图。

如果您未使用laravel 10,请简单地更改分支。例如,这里是laravel 5.5的文件夹

以下是laravel 10的默认分页,如果要使用POST请求,会是这样的:

resources/views/pagination/using-post.blade.php

@if ($paginator->hasPages())
    <nav>
        <ul class="pagination">
            {{-- 上一页链接 --}}
            @if ($paginator->onFirstPage())
                <li class="disabled" aria-disabled="true" aria-label="@lang('pagination.previous')">
                    <span aria-hidden="true">&lsaquo;</span>
                </li>
            @else
                <li>
                    {{-- <a href="{{ $paginator->previousPageUrl() }}" rel="prev" aria-label="@lang('pagination.previous')">&lsaquo;</a> --}}
                    <form action="{{ $paginator->previousPageUrl() }}" method="POST">
                        @csrf
                        <button type="submit" aria-label="@lang('pagination.previous')">&lsaquo;</button>
                    </form>
                </li>
            @endif

            {{-- 分页元素 --}}
            @foreach ($elements as $element)
                {{-- "三个点"分隔符 --}}
                @if (is_string($element))
                    <li class="disabled" aria-disabled="true"><span>{{ $element }}</span></li>
                @endif

                {{-- 链接数组 --}}
                @if (is_array($element))
                    @foreach ($element as $page => $url)
                        @if ($page == $paginator->currentPage())
                            <li class="active" aria-current="page"><span>{{ $page }}</span></li>
                        @else
                            {{-- <li><a href="{{ $url }}">{{ $page }}</a></li> --}}
                            <form action="{{ $url }}" method="POST">
                                @csrf
                                <button type="submit">{{ $page }}</button>
                            </form>
                        @endif
                    @endforeach
                @endif
            @endforeach

            {{-- 下一页链接 --}}
            @if ($paginator->hasMorePages())
                <li>
                    {{-- <a href="{{ $paginator->nextPageUrl() }}" rel="next" aria-label="@lang('pagination.next')">&rsaquo;</a> --}}
                    <form action="{{ $paginator->nextPageUrl() }}" method="POST">
                        @csrf
                        <button type="submit" aria-label="@lang('pagination.next')">&rsaquo;</button>
                    </form>
                </li>
            @else
                <li class="disabled" aria-disabled="true" aria-label="@lang('pagination.next')">
                    <span aria-hidden="true">&rsaquo;</span>
                </li>
            @endif
        </ul>
    </nav>
@endif

然后,当您使用links()方法时,您需要将您的自定义分页视图传递给Paginator。

<div class="container">
    @foreach ($users as $user)
        {{ $user->name }}
    @endforeach
</div>
 
{{ $users->links('pagination.using-post') }}
英文:

As Tim Lewis pointed out, the paginator by default works with GET requests. In order to make it use POST requests, you'd need to provide a custom view to the Paginator's links() method replacing every &lt;a&gt; tag with a &lt;form&gt;.

For example, the default view is visible in the github repo along with some others that use bootstrap or tailwind.

If you are not using laravel 10, simply change the branch. Here is the folder for laravel 5.5 for example

Here's what the default pagination for laravel 10 would look like if you make it use POST requests instead.

resources/views/pagination/using-post.blade.php

@if ($paginator-&gt;hasPages())
    &lt;nav&gt;
        &lt;ul class=&quot;pagination&quot;&gt;
            {{-- Previous Page Link --}}
            @if ($paginator-&gt;onFirstPage())
                &lt;li class=&quot;disabled&quot; aria-disabled=&quot;true&quot; aria-label=&quot;@lang(&#39;pagination.previous&#39;)&quot;&gt;
                    &lt;span aria-hidden=&quot;true&quot;&gt;&amp;lsaquo;&lt;/span&gt;
                &lt;/li&gt;
            @else
                &lt;li&gt;
                    {{-- &lt;a href=&quot;{{ $paginator-&gt;previousPageUrl() }}&quot; rel=&quot;prev&quot; aria-label=&quot;@lang(&#39;pagination.previous&#39;)&quot;&gt;&amp;lsaquo;&lt;/a&gt; --}}
                    &lt;form action=&quot;{{ $paginator-&gt;previousPageUrl() }}&quot; method=&quot;POST&quot;&gt;
                        @csrf
                        &lt;button type=&quot;submit&quot; aria-label=&quot;@lang(&#39;pagination.previous&#39;)&quot;&gt;&amp;lsaquo;&lt;/button&gt;
                    &lt;/form&gt;
                &lt;/li&gt;
            @endif

            {{-- Pagination Elements --}}
            @foreach ($elements as $element)
                {{-- &quot;Three Dots&quot; Separator --}}
                @if (is_string($element))
                    &lt;li class=&quot;disabled&quot; aria-disabled=&quot;true&quot;&gt;&lt;span&gt;{{ $element }}&lt;/span&gt;&lt;/li&gt;
                @endif

                {{-- Array Of Links --}}
                @if (is_array($element))
                    @foreach ($element as $page =&gt; $url)
                        @if ($page == $paginator-&gt;currentPage())
                            &lt;li class=&quot;active&quot; aria-current=&quot;page&quot;&gt;&lt;span&gt;{{ $page }}&lt;/span&gt;&lt;/li&gt;
                        @else
                            {{-- &lt;li&gt;&lt;a href=&quot;{{ $url }}&quot;&gt;{{ $page }}&lt;/a&gt;&lt;/li&gt; --}}
                            &lt;form action=&quot;{{ $url }}&quot; method=&quot;POST&quot;&gt;
                                @csrf
                                &lt;button type=&quot;submit&quot;&gt;{{ $page }}&lt;/button&gt;
                            &lt;/form&gt;
                        @endif
                    @endforeach
                @endif
            @endforeach

            {{-- Next Page Link --}}
            @if ($paginator-&gt;hasMorePages())
                &lt;li&gt;
                    {{-- &lt;a href=&quot;{{ $paginator-&gt;nextPageUrl() }}&quot; rel=&quot;next&quot; aria-label=&quot;@lang(&#39;pagination.next&#39;)&quot;&gt;&amp;rsaquo;&lt;/a&gt; --}}
                    &lt;form action=&quot;{{ $paginator-&gt;nextPageUrl() }}&quot; method=&quot;POST&quot;&gt;
                        @csrf
                        &lt;button type=&quot;submit&quot; aria-label=&quot;@lang(&#39;pagination.next&#39;)&quot;&gt;&amp;rsaquo;&lt;/button&gt;
                    &lt;/form&gt;
                &lt;/li&gt;
            @else
                &lt;li class=&quot;disabled&quot; aria-disabled=&quot;true&quot; aria-label=&quot;@lang(&#39;pagination.next&#39;)&quot;&gt;
                    &lt;span aria-hidden=&quot;true&quot;&gt;&amp;rsaquo;&lt;/span&gt;
                &lt;/li&gt;
            @endif
        &lt;/ul&gt;
    &lt;/nav&gt;
@endif

And then you'd need to pass this custom pagination view of yours to the paginator when you use the links() method.

&lt;div class=&quot;container&quot;&gt;
    @foreach ($users as $user)
        {{ $user-&gt;name }}
    @endforeach
&lt;/div&gt;
 
{{ $users-&gt;links(&#39;pagination.using-post&#39;) }}

huangapple
  • 本文由 发表于 2023年4月20日 05:36:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定