0

I'm making a filter for my products and I need to do something like http://example.com/products?type=laptop&cpu=i7. It's great but I need to do this with multiple select of specs. Maybe user wish to select core i7 and core i5 to compare the prices of laptops. Any idea how to do this RESTful on laravel?

2
  • I don't get it either. It's a perfectly legit question. Commented Feb 2, 2015 at 14:19
  • At a guess, it'll be because it's not specific to Laravel or really even REST. That would be a bit harsh in my opinion though. (It's also worth reading the hover text on both the up and down buttons to see the criteria for votes - it could be argued that the question doesn't show any research and that, to an extent, it's not amazingly clear. Still a bit harsh though!) Commented Feb 2, 2015 at 16:15

1 Answer 1

2

This is not Laravel-specific, but what you could do is set your mind on a delimiter (fx. a comma) and do a simple explode() to seperate them.

//http://example.com/products?type=laptop&cpu=i7,i5,amdathlon
$cpus = explode("," Input::get('cpu')); //Will produce an array ['i7','i5','amdathlon']

The other, more "native" way would be making an array in the actual URL, which would be done simply by adding [] to the name of your select (assuming your filters are made by a select-element)

<select name="cpu[]" multiple>
...
</select>

Which then would be accessible like this:

//http://example.com/products?type=laptop&cpu[]=i7&cpu[]=i5&cpu[]=amdathlon
$cpus = Input::get('cpu'); //['i7','i5','amdathlon']

And after that, you could do something like:

Cpu::whereIn('core', $cpus)->laptops; 

or whatever you're doing afterwards.

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

3 Comments

Yes, it's perfectly common practice. You could also do a pipe (|).
The second way is definitely more 'correct' (i.e. using URL params correctly rather than bodging strings into arrays). It does produce uglier URLs, of course, but I guess that's not a big deal.
Yes, I agree @alexrussell. The second way is more correct, but the comma-seperated one is prettier, more readable and I also more SEO-friendly (I suspect?)

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.