laravel分页和排序表

时间:2017-12-22 18:11:20

标签: php laravel sorting pagination

我正在使用排序功能和分页但是当我对任何列使用sort时的问题工作正常但是当我使用分页点击下一页时排序更改并且必须再次单击该列进行排序。 那么如何在使用分页时保持排序?

<tr>
    <th>
        <div class="checkbox checkbox-replace">
            <input type="checkbox" id="chk-3">

        </div>
    </th>

    <th>@sortablelink('details','Details')</th>
    <th>@sortablelink('postingdate','Date')</th>
    <th>@sortablelink('description','Description')</th>
    <th>@sortablelink('amount','Amount')</th>
    <th>@sortablelink('type','Type')</th>
    <th>@sortablelink('slip','Slip#')</th>
    <th>@sortablelink('vendor_id','Vendor')</th>
    <th>@sortablelink('category_id','Category')</th>
</tr>
public function index()
{
    $checks = Checks::orderBy('id', 'asc')->get();

    $checks= Checks::sortable()->paginate(5);

    return view('home',compact('checks'));
}

2 个答案:

答案 0 :(得分:3)

您可以通过执行以下操作将查询字符串附加到链接:

{{ $users->appends(['sort' => 'votes'])->links() }}

按可排序的查询替换sortvotes取代request()->sort

答案 1 :(得分:0)

根据this page,您还可以像这样进行分页:

//Controller
public function index()
{
    $checks = Checks::orderBy('id')->paginate(5);
    $links = $checks ->appends(['sort' => 'id'])->links();

    return view('home', compact('checks', 'links'));
}

在您看来,您可以像这样调用分页链接

{{ $links }}

希望有帮助。干杯!

相关问题