There's nothing wrong with passing variables containing the sanitized form input into your where() methods ... or am I missing the point?
Like how?
I was thinking like:
...
->where('ranks.id', '=', $rankID)
...
If this would work, how do I pass a '*'? because I tried:
...
->where('ranks.id', '=', *)
->where('ranks.id', '=', '*')
->where('ranks.id', '=', DB::raw(*))
->where('ranks.id', '=', DB::raw('*'))
...
none of which worked
If your ranks.id column is an integer you can't use an * in any form. Anything other than an integer or null would be a SQL error.
Are you trying to pass it as wildcard when no rank has been specified? If so, you would simply leave this portion of the query out when you have no value to filter on.
As the methods are chainable, you save the rest of the query to a temporary variable; wrap that where statement in an if clause and, if a rank has been input, apply the where method to the temporary variable.
You got to use function in where() I give you an example
->where(function($query ) use ($id_category)
{
if(Input::get('maxp'))
$query->whereBetween('price', array(Input::get('minp'), Input::get('maxp')));
})
Also you can go deeper and into the function of first where() you can put another where with function. Can make any code into the functions. Can iven get data from a table and condition with that data. Just take care of $query param.
I'm having a couple of those around, and solved it like this: http://laravel.io/bin/nya9 It's pretty ugly at some parts, but feels flexible as i'm just doing like with(new Companyselection\Query(Input::all())->count()); from the controller.
Basic idea is to abstract away all the ifs and deciding wheater its a between (i.e user gave both an starting and endpoint) / where / from to be able to more easily add field down the road.
The comments are in swedish, but i think method naming and code are pretty easy to follow along.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community