Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sitmgreg's avatar

combining sortby / pagination : best practices?

I'm curious how people solve the typical search / filter problem in Laravel. I see that paginate() nicely interprets the URL in the controller, https://laravel.com/docs/5.5/pagination#displaying-pagination-results hints that it's easy to add additional parameters to a paginated link (like sort).

Is this usually done by adding scopes to models and looking at Input::get()?

0 likes
1 reply
jlrdw's avatar

Watch this https://drive.google.com/file/d/0B1_PFw--3o74YjVreHNBOWU2aEE/view?usp=sharing

An example is using `like'

$dogsearch = (isset($_REQUEST['psch']) <> '' ?  Cln::fixValue($_REQUEST['psch']) : "");
$dogsch = $dogsearch . "%";
$aval = (isset($_REQUEST['aval']) <> '' ? Cln::fixValue($_REQUEST['aval']) : "");

change to laravel request if desired

and

$query = Dog::where('dogname', 'like', $dogsch);
        if ($aval == "n") {
            $query->where('adopted', '=', 1);
        } else if ($aval == "y") {
            $query->where('adopted', '=', 0);
        }
        $dogs = $query->orderBy('lastedit', 'DESC')->paginate(5);
        $pagelinks = array('psch' => $dogsearch, 'aval' => $aval);

and view

echo '<td>' . $dogs->appends($pagelinks)->links() . '</td>';

Of course all passed to view with ->with statements.

And the little form at top of list page to perform a search:

<div>
    <form method="post" action="dog/indexadmin">
    <!--<form method="post" action="<?php //echo DIR; ?>dog/indexadmin">-->
        <label>sch</label><input type="text" name="psch" value="">
        <label>Avail y or n</label><input type="text" name="aval" value="" size="10">
        <input type="submit" name="submit" value="Search">       
    </form>
</div>

Many ways to do this.

Please or to participate in this conversation.