【发布时间】:2020-05-02 20:37:07
【问题描述】:
如果我有一个 AJAX 调用返回一个名为 $selectArray 的数组,即 [foo,bar,foobar],如何在 Laravel 查询构建器中使用它以便对其进行清理?目前,我有
// Sanitizing Input
// $acceptableSelects is an array of columns in a table. Checks if there are any elements in $selectArray that is invalid
// If the check fails, it return the default array
$sanSelectArray = !array_diff($selectArray, array_keys($accetableSelects)) && count($selectArray) < 8 ? $selectArray : [foo,foo2,foo3]
foreach($sanSelectArray as $ele){
$query->addSelect('table.' . $ele); // Required since I have a LEFT JOIN
}
这似乎非常不直观。在没有多个 addSelect() 方法的情况下,有更好的方法来清理输入或将 Array 作为 SELECT 传递,但我找不到任何方法。
【问题讨论】:
标签: php mysql laravel laravel-query-builder