您好我正在尝试构建一个动态来自对象的参数。基本上我使用$key
作为列名称,使用$value
作为列的值。我认为键是准确的列,因为我自己设置它之前在PHP中传递它。这是我的PHP代码。
//model of the request Array [Object] - contains the filter list along with their key
$filtered_table = Method::leftJoin('users', 'users.id', '=', 'methods.created_by')
->leftJoin('roles', 'roles.id', '=', 'users.role_id')
->leftJoin('types', 'types.id', '=', 'methods.type_id');
foreach ($request->filters as $key => $value) {
$filtered_table->where($key, '=', $value);
}//dynamic where parameters
$filtered_table->get([ 'users.username', 'users.id AS users_id', 'methods.*', 'methods.id AS method_id', 'methods.name AS method_name', 'roles.id AS role_id', 'roles.name AS role_name',
'types.id AS type_id_typetable', 'types.name AS type_name']);
我在这里要做的是做多个->where
子句,以便我可以遍历该对象。但我不确定我的代码的语法。
以下是javascript中的类似行为,我分别使用了$ key和$ value。
for (var i=0; i < filtered_table.length; i++){
for (var key in filtered_table[i]){
console.log(key + " -> " + filtered_table[i][key]);
}
}
你能帮助我做一些像php那样的行为然后用它来获得动态的->where
参数吗?
修改
我遇到了问题,请看我的截图。
public function filterDataCal(Request $request) {
//model of the request Array [Object] - contains the filter list along with their key
$filtered_table = Method::leftJoin('users', 'users.id', '=', 'methods.created_by')
->leftJoin('roles', 'roles.id', '=', 'users.role_id')
->leftJoin('types', 'types.id', '=', 'methods.type_id')
->where(function ($filtered_table) use ($request->filters) {//this is line 122
foreach ($request->filters as $key => $value) {
$filtered_table->where($key, '=', $value);
}
})
->get([ 'users.username', 'users.id AS users_id', 'methods.*', 'methods.id AS method_id', 'methods.name AS method_name', 'roles.id AS role_id', 'roles.name AS role_name',
'types.id AS type_id_typetable', 'types.name AS type_name']);
if($filtered_table) {
return $filtered_table;//data model when returned Array [Object, Object, .....]
} else {
return "";
}
}
顺便说一下,结果是否仍然像我在那里所做的那样返回?
答案 0 :(得分:0)
事实证明我可以直接在where
中使用我的对象数组作为搜索地图,请参阅此处。
$filtered_table = Method::leftJoin('users', 'users.id', '=', 'methods.created_by')
->leftJoin('roles', 'roles.id', '=', 'users.role_id')
->leftJoin('types', 'types.id', '=', 'methods.type_id')
->where($request->filters)
->get([ 'users.username', 'users.id AS users_id', 'methods.*', 'methods.id AS method_id', 'methods.name AS method_name', 'roles.id AS role_id', 'roles.name AS role_name',
'types.id AS type_id_typetable', 'types.name AS type_name']);
参考
Laravel - extending Eloquent where clauses depending on dynamic parameters
答案 1 :(得分:0)
您可以concat
执行此操作,以便将基础查询的动态组合在一起:
$filtered_table = Method::leftJoin('users', 'users.id', '=', 'methods.created_by')
->leftJoin('roles', 'roles.id', '=', 'users.role_id')
->leftJoin('types', 'types.id', '=', 'methods.type_id');
foreach ($request->filters as $key => $value) {
$filtered_table .= $filtered_table->where($key, '=', $value);
}//dynamic where parameters use `concat` to make the query
$filtered_table->get([ 'users.username', 'users.id AS users_id', 'methods.*', 'methods.id AS method_id', 'methods.name AS method_name', 'roles.id AS role_id', 'roles.name AS role_name',
'types.id AS type_id_typetable', 'types.name AS type_name']);