Hi,
I have a form that collects data from three SELECT inputs, two of which can have multiple options selected. I can get the data in my Controller using Request, $request and data is like this:
array:4 [▼ // app/Http/Controllers/FieldProv.php:71
"_token" => "DK7cJuB6iJWTOh5OLqYR5jjxc8wE7X87HJCXxIGG"
"site" => "5MT10647"
"SUTyp" => array:3 [▶
0 => "GEN"
1 => "ISO"
2 => "NST"
]
"FeTyp" => array:3 [▶
0 => "APE"
1 => "ARP"
2 => "ASP"
]
]
I have a query that was written 10+ years ago that works fine but it was originally done on an ASP page. If I take it and run it just as a SQL test (not in my controller) with just a single value it works fine:
SELECT DISTINCT tblSU.Site,
tlkpSUTyp.SUTyp,
tblSU.SUNum,
tlkpSUDesc.SUDesc
FROM tlkpSUTyp
RIGHT JOIN (((tlkpSUDesc RIGHT JOIN tblSU ON tlkpSUDesc.SUDescCode = tblSU.SUDescCode)
LEFT JOIN tblPD ON (tblSU.SUNum = tblPD.SUNum)
AND (tblSU.Site = tblPD.Site))
LEFT JOIN tblFeature ON (tblPD.FeNum = tblFeature.FeNum)
AND (tblPD.SUNum = tblFeature.SUNum)
AND (tblPD.Site = tblFeature.Site))
ON tlkpSUTyp.SUTypCode = tblSU.SUTypCode
WHERE tblSU.Site Like ‘123’
AND tblSU.SUTypCode LIKE ’syt’
AND tblFeature.FeTypCode LIKE ‘ert’
ORDER By tlkpSUTyp.SUTyp ASC,
tblSU.SUNum ASC
The values for AND tblSU.SUTypCode LIKE ’syt’ AND tblFeature.FeTypCode LIKE ‘ert’ can have multiple options from the SELECT input on the form.
So I have the query in my controller like this and it works when a single option is chosen, I just need the multiple options that are in the array above:
$site = $request->input('site');
$suTyp = $request->input('SUTyp');
$suTyp = implode(" ", $suTyp);
$feTyp = $request->input('FeTyp');
$feTyp = implode(" ", $feTyp);
$siteSql = "SELECT DISTINCT tblSU.Site, tlkpSUTyp.SUTyp, tblSU.SUNum,
tlkpSUDesc.SUDesc
FROM tlkpSUTyp
RIGHT JOIN (((tlkpSUDesc RIGHT JOIN tblSU ON tlkpSUDesc.SUDescCode = tblSU.SUDescCode)
LEFT JOIN tblPD ON (tblSU.SUNum = tblPD.SUNum)
AND (tblSU.Site = tblPD.Site))
LEFT JOIN tblFeature ON (tblPD.FeNum = tblFeature.FeNum)
AND (tblPD.SUNum = tblFeature.SUNum)
AND (tblPD.Site = tblFeature.Site))
ON tlkpSUTyp.SUTypCode = tblSU.SUTypCode
WHERE tblSU.Site Like :id AND tblSU.SUTypCode LIKE :suTyp AND tblFeature.FeTypCode LIKE :feTyp
ORDER By tlkpSUTyp.SUTyp ASC";
tblSU.SUNum ASC";
$siteSql = DB::getPdo()->prepare($siteSql);
$siteSql->setFetchMode(\PDO::FETCH_OBJ);
$siteSql->execute([
'id' => $site,
'suTyp' => $suTyp,
'feTyp' => $feTyp
]);
$site = collect($siteSql->fetchAll());
So I was thinking that DB Query Builder might be the way to go (instead of this way) to handle the multiple options using whereIn() but I have no idea how to handle the those complicated Joins. Or maybe I am close with what I have and this just needs a few changes.
Thanks,
Stan