php - Advanced search query using multiple, and varied queries -
php - Advanced search query using multiple, and varied queries -
currently, user can add together criteria searched amongst our database find matching results. working perfectly, however, there need 'enhance' search. help create things clearer, there working illustration below:
scenario
a user wants buy
property:
location
which has 4
bedrooms is detached
within cost range of $300k - $500k. this added database, , function below finds matching properties (shortened):
$locations = location::wherehas('coordinate', function ($q) utilize ($bedrooms, $property_type) { $q->where('bedrooms', '=', $bedrooms) ->where('type', '=', $property_type); })->lists('id');
so can see, query pretty 'set' on querying one
number of bedrooms , one
property type' - it's 1 variable on same row!
now, problem.
currently, user can add together 1 preference of bedrooms, type of house
etc... alter add together multiple choice
bedrooms, type of house
etc... new query
user:
proposed scenario
withinlocation
(this 1 location) which has 3,4,5,6
bedrooms is detached, studio, semi-detached, bungalow
within cost range of $300k - $500k. (always range). my question advice, how can store such query, , query homecoming properties match criteria. each criteria exists on 1 row, imagine i'll have have separate table store number of bedrooms etc...?
also, example, if 1 user wants 1
bedrooms , detached, studio, semi-detached, bungalow
, opposed user criteria may far more in depth, how can query accommodate this?
instead of redesigning tables, not serialize number of bedrooms etc.. , store them in varchar, , un-serialize them on request?
i hope i've explained things clearly, including providing current code. if require farther info please allow me know. many in advance.
you utilize logic command clauses depending on values set and/or if array. instead of chaining statements write:
//bedrooms (required, mixed) if(is_array($bedrooms)){ $q->wherein('bedrooms', $bedrooms) }else{ $q->where('bedrooms', '=', $bedrooms) } //type (required, mixed) if(is_array($property_type)){ $q->wherein('type', $property_type) }else{ $q->where('type', '=', $property_type) } //minimum cost (optional, int) if(isset($min_price)){ $q->where('price', '>=' , $min_price) } //maximum cost (optional, int) if(isset($max_price)){ $q->where('price', '<=' , $max_price) }
this fast illustration based on provided see how refactor phone call or values passing query seek , increment code readability.
php mysql sql laravel laravel-4
Comments
Post a Comment