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.rdiz– rdiz2015-02-02 14:19:52 +00:00Commented 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!)alexrussell– alexrussell2015-02-02 16:15:20 +00:00Commented Feb 2, 2015 at 16:15
Add a comment
|
1 Answer
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.
3 Comments
rdiz
Yes, it's perfectly common practice. You could also do a pipe (
|).alexrussell
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.
rdiz
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?)