Laravel (3) Pagination Sorting & Filtering

半世苍凉 提交于 2019-12-03 15:54:38

This is easy to do in laravel thanks to the great pagination and input class. Try this in your views:

Example View

Cleaned up view

<form action="" method="get" id="filter">

    Show <select name="game_id">
            <option value="">All</option>
        <?php foreach ($games as $game):?>
            <option value="<?=$game->id?>" <?=($game->id == Input::get('game_id')) ? 'selected="selected"' : null?>><?=$game->name?></option>
        <?php endforeach;?>
    </select>

    Show <select name="server_id">
            <option value="">All</option>
        <?php foreach ($servers as $server):?>
            <option value="<?=$server->id?>" <?=($server->id == Input::get('server_id')) ? 'selected="selected"' : null?>><?=$server->name?></option>
        <?php endforeach;?>
    </select>

    <input type="submit" value="Filter" rel="filter">

</form>

<hr>

<?php if (count($servers) > 0):?>

    <?=$pagination?>

    <table>
        <tr>
            <th><a href="<?=URL::to('servers?sort=id'.$querystr)?>">ID</a></th>
            <th><a href="<?=URL::to('servers?sort=rank'.$querystr)?>">RANK</a></th>
            <th><a href="<?=URL::to('servers?sort=date'.$querystr)?>">DATE</a></th>
            <th><a href="<?=URL::to('servers?sort=language'.$querystr)?>">LANGUAGE</a></th>
            <th><a href="<?=URL::to('servers?sort=uptime'.$querystr)?>">UP TIME</a></th>
            <th><a href="<?=URL::to('servers?sort=votes'.$querystr)?>">VOTES</a></th>
        </tr>
        <tr>
            <td>
                ...
            </td>
        </tr>
    </table>

    <?=$pagination?>

<?php else:?>
    <h2>No results found.</h2>
<?php endif;?>

Example Controller

public function get_action()
{
    // CACHE SORTING INPUTS
    $allowed = array('rank', 'date', 'language', 'uptime', 'votes'); // add allowable columns to search on
    $sort = in_array(Input::get('sort'), $allowed) ? Input::get('sort') : 'id'; // if user type in the url a column that doesnt exist app will default to id
    $order = Input::get('order') === 'asc' ? 'asc' : 'desc'; // default desc

    $servers = Server::order_by($sort, $order);

    // FILTERS
    $game = null;
    $server = null;

    if (Input::has('game_id')) {
        $servers = $servers->where('game_id', Input::get('game_id'));
        $game = '&game_id='.Input::get('game_id');
    }
    if (Input::has('server_id')) {
        $servers = $servers->where('server_id', Input::get('server_id'));
        $server = '&server_id='.Input::get('server_id');
    }

    // PAGINATION
    $servers = $servers->paginate(5);

    $pagination = $servers->appends(
        array(
            'game_id'       => Input::get('game_id'),
            'server_id' => Input::get('server_id'),
            'sort'      => Input::get('sort'),
            'order'     => Input::get('order')
        ))->links();

    return View::make(servers.list)->with(
        array(
            'game'          => null,
            'servers'       => $servers,
            'pagination'    => $pagination,
            'querystr'      => '&order='.(Input::get('order') == 'asc' || null ? 'desc' : 'asc').$game.$server
        ));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!