Stephenmelb
Stephenmelb

Reputation: 457

Laravel: How to Append additional items to URL query string

I have a form that takes two fields

SUBURB and STATE

and submits the fields to the URL eg.

www.mysite.com/?suburb=Manhatten&state=NY

In My Laravel Controller I have

 if (isset($input['state']) && $input['state'] != '')
        {
            $query = $query->where('state', 'like', '%'. $input['state'].'%');
        }
 if (isset($input['suburb']) && $input['suburb'] != '')
        {
            $query = $query->where('suburb', 'like', '%'. $input['suburb'].'%');
        }

I want to allow my users to add additional SUBURBS or STATES so they can get more results eg.

www.mysite.com/?suburb= Manhatten OR Brooklyn OR Queens &state= NY OR NJ

I don't know how I can setup my form to append a 'OR' to the additional suburbs and how do I setup my controller to look for the additional values?

Upvotes: 0

Views: 1699

Answers (1)

The Alpha
The Alpha

Reputation: 146191

You don't need to add anything extra to your controller for adding query string, just add the query stqing in the form action, for example:

Form::open(array('url' => 'search/?suburb=Manhatten/Brooklyn/Queens&state=NY', 'method' => 'post'))

Then in your Controller method:

if ($state = Input::get('state')) {
    $query = $query->where('state', 'like', '%'. $state .'%');
}

if ($suburb = Input::get('suburb')) {
    $suburbs = explode('/', $suburb);
    $query = $query->where('suburb', 'like', '%'. array_shift($suburbs) .'%');
    if(count($suburbs)) {
        foreach($suburbs as $sub) {
            $query = $query->orWhere('suburb', 'like', '%'. $sub .'%');
        }
    }
}

Upvotes: 1

Related Questions