You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 8+Vue多参数过滤问题:如何传递全部filtro数组参数

Laravel + Vue 多参数表格筛选实现方案

前端(Vue)修改

先确保你的filtro对象包含所有需要的筛选字段(id、id_status、id_input等),然后修改axios请求,传递完整的筛选参数:

// 示例filtro定义
data() {
  return {
    filtro: {
      id_solicitud: '',
      id: '',
      id_status: '',
      id_input: ''
      // 其他需要的筛选字段
    }
  }
}

// 发起筛选请求的方法
async loadFilteredTable() {
  try {
    // 可选:过滤掉空值参数,避免传递无效值
    const validFilters = Object.fromEntries(
      Object.entries(this.filtro).filter(([_, val]) => val !== '' && val !== null)
    )

    const res = await axios.get('/api/your-endpoint', {
      params: validFilters // 传递所有有效筛选参数
    })
    this.tableData = res.data
  } catch (err) {
    console.error('筛选请求出错:', err)
  }
}

后端(Laravel)修改

在控制器的GetFiltro方法中,动态根据请求参数构建查询条件:

public function GetFiltro(Request $request)
{
    $query = YourModel::query();

    // 白名单定义允许的筛选字段,防止非法参数注入
    $allowedFields = ['id_solicitud', 'id', 'id_status', 'id_input'];

    // 循环处理每个筛选字段,仅当参数存在且非空时添加查询条件
    foreach ($allowedFields as $field) {
        if ($request->filled($field)) {
            $query->where($field, $request->input($field));
        }
    }

    // 可按需添加分页、排序逻辑
    $results = $query->paginate(15);

    return response()->json($results);
}

如果需要模糊匹配(如关键词搜索),可以单独处理特定字段:

// 示例:对某个字段做模糊查询
if ($request->filled('keyword')) {
    $query->where('title', 'like', "%{$request->input('keyword')}%");
}

API路由确认

确保你的路由已正确指向该控制器方法:

Route::get('/your-endpoint', [YourController::class, 'GetFiltro']);

内容的提问来源于stack exchange,提问作者Eduardo Aroca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 13:02:42