t0berius's avatar

return view with Query

I'm looking for a way to get the search parameter which might be passed as a GET parameter OR as a POST parameter to my searchController

    //get parameter set
    if($request->filled('q')){

        $searchString = $request->q;
    }
    else{

        $searchString = $request->search;
    }

//logic....

return view('searchResults',[])->withQuery($searchString . "xx");

For some reason this doesn't work as expected, any idea? Using POST paramaters the values are not passed to the URL.

0 likes
3 replies
jlrdw's avatar

In the method get the passed query string parameters if any, example

public function indexAdmin()
    {
        $dogsearch = !empty(Request::input('psch')) ? Request::input('psch') : '';
        $aval = !empty(Request::input('aval')) ? Request::input('aval') : '';

Then a params array can be built:

$params = ['psch' => $dogsearch, 'aval' => $aval];

Pass to view:

return view('dog.index', compact('dogs', 'params'));

And view for paginator:

{{ $dogs->appends($params)->links() }}

The params make querystring like:

somesite.com/dog?page=2$aval=1&psch=ch
// just example

Not sure what your if else is doing.

You have to get the request parameters each time, unless the parameters are a constant for that query.

In example, they are not constant, a user could type r, ro, for rover, or whatever.

Now if I had a query with only "show me the dogs available for adoption" then parameter in url would not be required.

This would work:

$dogs = DB::table('dc_dogs')->where('adopted', '=', 0)->paginate(5);

The only query parameter would be the page=whatever.

'adopted', '=', 0 hard coded in.

t0berius's avatar

@jlrdw

I mean the URL inside the browser, not the pagination URL. Background:

I offer a form where a user can filter by entering a keyword and some additional filter options. I only want the keyword to be included into the URL query after the POST request has been submitted.

-Additional parameters will be stored in session and should not be included into URL after form submit.

jlrdw's avatar

Use a flag:

Look at my last reply here: https://laracasts.com/discuss/channels/guides/length-aware-paginator

On initial request store all in the sessions. A flag (if construct) checks if page is greater than one. If so use the parameters from session.

In my example, I count the number of records if page = 1, otherwise count is retrieved from session.

What you need is similar. And page is a perfect thing to test on.

Note this part:

if (!$page === '1') {
            $numrows = Session::get('numrows');  //got from session, already counted
        } else {
        // more

Only pitfall is if returning to page one, could break. So you made need a different flag, like something in session. And test to see if it is set or a certain thing. Otherwise the parameters are lost.

If not sensitive parameters, I'd just use query string.

The query string can have blanks:

site.com?page=5&something=&whatever=2&c=bob

You don't error on empty query string parameters.

However, the query has to account for parameters one way or the other.

Look at query scopes, may help.

EDIT: Also see https://stackoverflow.com/questions/24891276/how-to-automatically-append-query-string-to-laravel-pagination-links

You can use the request in the paginator appends, I've just never tried that. Skip the token.

Please or to participate in this conversation.