Question

the issue is that if the user does not choose some values in the searchform, (because he may want broad search) the WHERE clauses will fail. For example, if he does not choose construction year for the house, then, since all the houses have a construction year, nothing will be shown. or if he does not choose a price limit for the house, then, paradoxically no results will be shown because no houses cost less than nothing.

I solved that issue in codeigniter where I could code IF clauses within the sql query, but here in Laravel I just dont know how to go about it. I have tried many syntaxes, like

->if($priceto != "")
{
->where('price', '<',$priceto)

}

but all fail.

So here are some input values from the form. Think that some may be unfilled

                    $country        = Input::get('country');
                    $region         = Input::get('regions');
                    $town           = Input::get('town');
                    $transaction    = Input::get('transaction');
                    $pricefrom      = Input::get('pricefrom');
                    $priceto        = Input::get('priceto');
                    $builtyear      = Input::get('builtyear');



                }


                $users = DB::table('users')
                ->join('properties', 'users.id', '=', 'properties.id_user_fk')
                ->select('users.email', 'properties.country', 'properties.region',             'properties.town',            'properties.price', properties.rooms','properties.m2','properties.swimming',
                         'properties.garden','properties.garage','properties.builtyear','properties.pic1',
                         'properties.pic2')


here below is where the problems start, because of what mentioned, what if the variables $priceto or $builtyear have nothing in them, nothing is shown

                ->where('country', '=', $country)                        
                ->where('region', '=', $region)
                ->where('town', '=', $town)
                ->where('transaction', '=', $transaction)
                ->where('price', '<', $priceto)
                 etc etc

I also thought of checking what variables have a value and push them into an array before starting the sql query, but the problem is then reading the array from the sql query.

Was it helpful?

Solution

What Felix says is a good way to work. Alternatively, you can chain your query as such:

// start off your query
$users = DB::table('users');

if ($country != false) {
   $users = $users->where('country', '=', $country);
}

if ($priceto != false) {
   $users = $users->where('price', '>', $priceto);
}


// other conditional statements here


// grab your final results
$users = $users->get();

Hope that helps!

OTHER TIPS

The Input::get() method accepts an optional second parameter which specifies the default value (if the parameter is not present.

Input::get('priceto', 0); will give you the value of the priceto form field or 0 if none is present.

You can do the same is laravel as in codeigniter.

ie

$users = DB::table('users')
                ->join('properties', 'users.id', '=', 'properties.id_user_fk')
                ->select('users.email', 'properties.country', 'properties.region',             'properties.town',            'properties.price', properties.rooms','properties.m2','properties.swimming',
                         'properties.garden','properties.garage','properties.builtyear','properties.pic1',
                         'properties.pic2');

if($priceto != "")
{
$users = $users->where('price', '<' ,$priceto);

}

$users = ......... // continue with the statements

$users = $users->get();

Its just chaining your query.

You should only add the ->where() if variable is set, like this:

$users = DB::table('users')
    ->join('properties', 'users.id', '=', 'properties.id_user_fk')
    ->select('users.email', 'properties.country', 'properties.region',             'properties.town',            'properties.price', properties.rooms','properties.m2','properties.swimming',
                     'properties.garden','properties.garage','properties.builtyear','properties.pic1',
                     'properties.pic2');

if (!empty($country)) {
    $users->where('country', '=', $country);
}
if (!empty($region)) {                      
    $users->where('region', '=', $region);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top