user9082946
user9082946

Reputation:

laravel pagination and sort table

I'm using sort function and pagination but the problem when I use sort for any column works fine but when I click to the next page using pagination the sort change and have to click on the column again for sorting. so how to keep sorting while using pagination?

<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'));
}

Upvotes: 1

Views: 17419

Answers (4)

dark_coder
dark_coder

Reputation: 11

you can use this line in your blade file

{!! $product->appends(\Request::except('page'))->render() !!}

Upvotes: 0

Chrisphine
Chrisphine

Reputation: 164

{{links()}} will still give a messed up sort when you paginate. This is the same as {!! link() !!} also and {{ $checks->links()}} Rather, use the code below. It will correct the error.

//for the Controller
public function index()
{
    $checks = Checks::sortable()->paginate(5);

    return view('home', compact('checks'));
}
//for the blade template in laravel append this in  div after the table

{!! $checks->appends(\Request::except('page'))->render() !!}][1]

Upvotes: 0

Qrazier
Qrazier

Reputation: 182

According to this page, you also can do your pagination like this:

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

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

And in your view, you can just call your pagination links like this

{{ $links }}

Hope it helps. Cheers!

Upvotes: 4

Mathieu Ferre
Mathieu Ferre

Reputation: 4412

You can append query string to your link by doing this :

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

replace sort by your sortable query, and votes by something like request()->sort

Upvotes: 6

Related Questions