0

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?

1
  • You would have to do something custom. For instance users would have to say OR, and then you can use explode(' OR ', $input['state']) (then loop through the words and create a long query). Commented Apr 9, 2014 at 1:40

1 Answer 1

1

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 .'%');
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.