0

I'm following this tutorial to make filters on my site.

I'm trying to add an OR statement within the filter in the HTML.

For example,

<a href="{{ route('rooms.index',
    ['type' => request('type'),
    'sort' => request('sort'),
    'region' => 'Lisbon'|'Lisboa',
    ]) }}">Lisbon</a>

However, if I do this, only results for "Lisbon" appear, but those for "Lisboa" do not.

I have also tried

'region' => ('Lisbon|Lisboa'),
'region' => ('Lisbon||Lisboa'),
'region' => array('Lisbon','Lisboa'),
['region' => 'Lisbon', 'region' => 'Lisboa',],

all with no luck. It seems like when there is a comma between "Lisbon" and "Lisboa" that the operator only looks at the first option, "Lisbon" in this case.

(If you're wondering, Google Maps API gives me either Lisbon or Lisboa for the admin district, even though I have restricted it to English.)

Then, for my controller, here is how I am doing the filters:

  public function index()
    {
        $rooms = new Room;
        if (request()->has('type')) {
            $rooms = $rooms->where('type', request('type'));
        }
        if (request()->has('region')) {
            $rooms = $rooms->where('region', request('region'));
        }
        if (request()->has('sort')) {
            $rooms = $rooms->orderBy('created_at', request('sort'));
        }
        $rooms = $rooms->paginate(20)->appends([
          'type' => request('type'),
          'region' => request('region'),
          'sort' => request('sort'),
        ]);
        return view('rooms.index')->with('rooms', $rooms);

Any idea what could be a possible solution here?

Thanks!

4
  • The code you presented are just defining route parameters, not anything to do with the actual filtering of data. Commented Sep 24, 2017 at 14:21
  • True. Thanks @Devon I will add the content from my controllers. Commented Sep 24, 2017 at 14:22
  • Also, not sure what syntax you are trying to use with ('Lisbon')|('Lisboa'). This evaluates to a string of Lisboo. Commented Sep 24, 2017 at 14:24
  • To be honest, @Devon , I was trying several different ways and made a few up along the way... Sorry if that was a dumb attempt. Commented Sep 24, 2017 at 14:27

1 Answer 1

1

Laravel collections include a whereIn() method to filter against arrays. where() only accepts one value.

https://laravel.com/docs/5.5/collections#method-wherein

Therefore, if you are passing in an array of data as the region, you could do:

$rooms = $rooms->whereIn('region', request('region'));

Furthermore

A string of 'Lisbon|Lisboa' is a string, not an expression. You should not expect any code to understand that as two separate values. Wrapping it in parentheses doesn't change anything, parentheses are used to provide an order to the operations, like in math.

'Lisbon'|'Lisboa' is a bitwise operation as | is a bitwise operator and not what you are looking for. || is an OR operator, but you can't just pass an expression as a parameter, you'll just be passing what that expression evaluates to which would be true or false if you use ||.

When dealing with a list of values, you almost always want to utilize PHP arrays and array functions. in_array is the underlying function that powers this filter. Be sure to understand arrays and review the available functions here.

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

3 Comments

Thanks @Devon, but for some reason, I keep getting an error: (1/1) ErrorException Invalid argument supplied for foreach()
Sounds like you aren't passing an array
You're absolutely right! Sorry about that. So, I did it like this and it worked: 'region' => ['Lisbon','Lisboa'],

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.