I have 2 variables that can either be filled with a value, be 'all' or be 'null'
Variable 1 is $type
and variable 2 is $brand
.
So if the user selects a type and a brand I want to query the cars accordingly. If the users selects nothing then the value is 'null' if the user selects 'all' then the value is 'all' obviously.
I've wrote the 4 scenario's out in commented code as follows:
//brand and type -> get cars of brand and type
//brand = all or null and type = all or null -> get all cars
//brand and type = all or null -> get cars of all types and brand
//brand = all or null and type -> get cars of all brands and type
this is my if structure:
if ($brand && $type) {
$cars = Cars::where([
['cost', '<=', $budget],
['brand', '=', $brand],
['type', '=', $type]
])->get();
} elseif($brand == 'all' || $brand === null && $type == 'all' || $type === null) {
$cars = Cars::where('cost', '<=', $budget)
->get();
} elseif($brand && $type == 'all' || $type === null) {
$cars = Cars::where([
['cost', '<=', $budget],
['brand', '=', $brand],
])->get();
} elseif ($brand == 'all' || $brand === null && $type) {
$cars = Cars::where([
['cost', '<=', $budget],
['type', '=', $type]
])->get();
}
Now when I check the value of $brand
it's filled with Audi
and $type
is null
.
Can someone please explain to me why I get the result of the first elseif? So my code thinks this is the case:
elseif($brand == 'all' || $brand === null && $type == 'all' || $type === null) {
Which is weird because $brand
is neither 'all' or 'null' since it's 'Audi'
Hope it's clear.
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ALAmG2
via IFTTT
Aucun commentaire:
Enregistrer un commentaire