0

I have a search query in my controller

public function byQuery($query)
    {
        $q = $this->prepareQuery($query);
        if(str_contains($q,'zealand'))
        {
        $q = 'nz';
        }
        $titles = Title::where('suburb', 'LIKE', $q)
                                ->orWhere('postcode', 'LIKE', $q)
                                ->orWhere('title', 'LIKE', $q)
                                ->orWhere('country', 'LIKE', $q)
                                ->orWhere('state', 'LIKE', $q);

        if (isset($input['type']) && $input['type'] != '')
        {
            $titles = $titles->where('type', 'like', '%'. $input['type'].'%');
        } 
            $titles = $titles->orderBy('href', 'ASC')
                    ->orderBy(DB::raw('RAND()'))
                    ->paginate(36);

            return $titles;

    }

If I search for search like

search?q=new zealand

It will return all the results in my DB which match new zealand

If I search for

   search?q=new zealand&type=sold

I want if to return all the results from my DB which match new zealand and the type is sold.

So far it's not working. It only returns all the results from my DB and doesn't limit them to just the sold results.

1 Answer 1

2

$input is not defined (at least, I don't see it in the code you posted), so the isset() will always return false. Use the Input class, which already can do the checking:

if (Input::has('type'))
{
   $titles = $titles->where('type', 'like', '%'. rawurldecode(Input::get('type')).'%');
} 

Note: I added a rawurldecode() since from your example 'new zealand' would be passed down as 'new+zealand', and this needs be taken into account if you want it to match correctly.

Sign up to request clarification or add additional context in comments.

7 Comments

Hi Damien thanks for the help. I've added the INPUT class to my namespace and replaced the old input code with yours, but it's still not returning the right results?
If you run the generated query against the db does it find results?
yes.. i have four types sold,buy,rent,offer they all have results, but the query will not separate them, it'll return all four types
It might mean you're not building the query correctly (check the generated sql)
Hi Damien could it be I'm using ->orWhere('postcode', 'LIKE', $q) ->orWhere('title', 'LIKE', $q) ->orWhere('country', 'LIKE', $q) ->orWhere('state', 'LIKE', $q); so the query will return where the type is 'sold' orwhere country like new zealand and type is 'buy'?
|

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.