mistery_girl's avatar

Route for search pagination

Hello! I'm using Laravel 5. I'm trying to make a route for pagination results from a search. My url when there is pagination is like this: myproject/search?q=something/page/2

My routes are:

Route::get('/search',['uses' => 'SearchController@index','as' => 'search']);```

First route throws `NotFoundHttpException`. Second is fine.

My form is:
```  {!!Form::open(['url' => '/search', 'method' => 'get', 'class' => 'search-bar'])!!}
 <input type="text" placeholder="Search"  name="q" >
{!!Form::submit( '',['type' => 'submit'])!!}
        {!!  Form::close() !!}```

And when form submits, url is:
myproject/search?q=something

This is fine, but I can't make route for search pagination. It should look like:
myproject/search?q=something/page/1

How to do that? Thanks!


0 likes
24 replies
mistery_girl's avatar

Thank you for your answer, but I've made custom pagination. I don't want to use Laravel pagination. Is there a way to make a route for url like that:

"myproject/search?q=something/page/1"

jlrdw's avatar

Read higher in that post, there's an example of another paginator.

jlrdw's avatar

You have to use request in controller to get the value of something. Look at request and controllers in the docs.

bestmomo's avatar

You should use this kind of url :

myproject/search?q=something&page=1
1 like
mistery_girl's avatar

Thank you, I'll try it. In my controller I get value in this way: $keyword= Input::get('q'); But problem is that it can't find route for that.

If I use this kind of url as @bestmomo said:

myproject/search?q=something&page=1

In controller how to get value from page?

jlrdw's avatar

Just do a regular route to controller.

jlrdw's avatar

Like


public function petlist()
{
    if(isset($_REQUEST['t1'])) // replace with   {Input::get('q')        
         $t1 = $_REQUEST['t1'];
    }
    else
    {
        $t1 = "";
    }

Don't attempt to pass querytring in the route, you pick up those values inside the controller method. Really this is a very simple thing.

jlrdw's avatar

What I meant is a regular route like:

Route::get('pets', array('uses' => 'PetsController@petlist'));

Nothing special needs to be done to get the querystring vars in the controller. They will be there.

mistery_girl's avatar

But if my url is:

myproject/search?q=something&page=1

In Controller can I get values from what is searched and number of page in this way? public function index() { if(isset($_REQUEST['q']))
$q = $_REQUEST['q']; }

if(isset($_REQUEST['page']))
$q = $_REQUEST['page']; } }

And my route will be: Route::get('search/{{search}}/', array('uses' => 'SearchController@index'));

Is this right?

jlrdw's avatar

You dont route a querystring
not

Route::get('search/{{search}}/', array('uses' => 'SearchController@index'));

this

Route::get('search', array('uses' => 'SearchController@index'));

Get the querystring stuff in the controller using Input::get.
You only need the other way if pretty url's, I use querystrings.

mistery_girl's avatar

And if I use route as you say, my url could be like that I posted? I'll try it!

mistery_girl's avatar

Route is not correc. I use this: Route::get('search', array('uses' => 'SearchController@index')); and in controller I have:


        if(isset($_REQUEST['q'])) {
            $keyword = $_REQUEST['q'];
        }

        if(isset($_REQUEST['page'])) {
            $page = $_REQUEST['page'];
        }
//some other code
}
and even when my url is:
"myproject.com/public/search?q=audi&page=2"

it throws exception. How it should be?

jlrdw's avatar

Don't use request like that. Remember Laravel 4.2 an Laravel 5 is different:

Laravel 5.1
$name = $request->input('name');
Laravel 4.2
$name = Input::get('name');

Try one of these types depending on your laravel version. And If ver 5.1 you need at top of controller with other use statements:

use Illuminate\Http\Request;

and in method

public function index(Request $request)

Let me know if you are using ver 4.2 EDIT: I see you are using L5, sorry, above should work then.

mistery_girl's avatar

I'm using Laravel 5.1. In controller I use Illuminate\Http\Request; Now, my controlelr is: public function index(Request $request){ $keyword = $request->input('q'); $page = $request->input('page');

//other code } My route is: Route::get('/search', array('uses' => 'SearchController@index')); and my url is: myproject/public/search?q=something&page=3 and route is not found.

jlrdw's avatar

Look at how Jeffery / Taylor has request usage in the Auth or User controller, that's how it's done.

jlrdw's avatar

Post code like

Example One: Basic

```
public function doSomething() {
return $this->doesSomethingElse();
}
```

This will be rendered like so:
public function doSomething() {
    return $this->doesSomethingElse();
}

3 ticks before code and 3 after.
And post your routes, view, and controller, and model. Something isn't right.

mistery_girl's avatar

Maybe my model is not right. My current route is: Route::get('/search', array('uses' => 'SearchController@index')); My controller:

 $home_articles = new CModel();
 $articles = $home_articles->getArticlesBySearch(14, $offset, false, $q);
}```
My model is:

namespace App\Models;

use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Redirect; use Cache; use DB; use Illuminate\Support\Facades\Request; use Input; class CModel extends Model { public function pagination ($page, $total_pages, $category, $type = false) { $segment = Request::segment(1); $keyword= Input::get('q'); //some code for pagination pages foreach ($links as $key){ if($type) { if ($key == 1) { $res .= ''.$key.''; } else { $res .= '' . $key . ''; }

                } 

else { //some code } } }```

jlrdw's avatar

Make you model look like:

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class Powner extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{

    use Authenticatable, Authorizable, CanResetPassword;

    protected $table = 'powners';
    protected $primaryKey = 'ownerid';
    protected $fillable = [
        'ownerid',
        'oname',
        'ostreet',
        'odate',
        'ocheck',
        'ntest'
    ];
    public $timestamps = [];

}

But using your names and data fields. Back yours up first. And please format your code like in previous section I showed.

jlrdw's avatar

At this point, I'd say watch some laracast videos. On using controllers and models.

mistery_girl's avatar

Ok, I'll see how to do it. This example doesn't have much similar to my model. I use it in this way but I'm not sure how to pass parameters $q which is what is searched and $page - number of page from pagination. I will see it. Thanks.

Please or to participate in this conversation.