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

在Laravel+Inertia/Vue中实现客户列表实时搜索的最佳方案

实现客户列表实时搜索功能(Laravel + Inertia + Vue3)

步骤1:前端Vue添加搜索框与实时请求逻辑

修改你的Vue组件,添加搜索输入框,用watch监听输入变化,通过Inertia路由实时请求后端数据:

<script setup>
import { usePage, router } from '@inertiajs/vue3'
import Pagination from '@/mycompany/Pagination.vue';

const page = usePage()
const searchTerm = ref('')
// 默认搜索编号列,可根据需求调整
const searchColumn = ref('nr')

// 监听搜索词变化,实时发起请求
watch(searchTerm, (newTerm) => {
  router.visit(route('customer.search', {
    searchColumn: searchColumn.value,
    searchTerm: newTerm,
    page: 1 // 搜索时重置到第一页,避免空分页
  }), {
    replace: true // 替换历史记录,优化浏览器回退体验
  })
})
</script>

<template>
  <!-- 顶部搜索区域 -->
  <div class="mb-4 flex items-center">
    <input
      v-model="searchTerm"
      type="text"
      placeholder="输入关键词搜索客户..."
      class="px-4 py-2 border rounded-md"
    />
    <!-- 可选:添加搜索列切换下拉框 -->
    <select 
      v-model="searchColumn" 
      class="ml-3 px-3 py-2 border rounded-md"
    >
      <option value="nr">客户编号</option>
      <option value="name">客户名称</option>
      <!-- 按需添加其他可搜索字段 -->
    </select>
  </div>

  <!-- 客户列表表格 -->
  <table class="min-w-full border-collapse">
    <thead>
      <tr class="bg-gray-100">
        <th class="px-4 py-2 text-left">客户编号</th>
        <th class="px-4 py-2 text-left">客户名称</th>
        <!-- 其他表格列 -->
      </tr>
    </thead>
    <tbody>
      <tr 
        v-for="customer in $page.props.customers.data" 
        :key="customer.id"
        class="border-b"
      >
        <td class="px-4 py-2">{{ customer.nr }}</td>
        <td class="px-4 py-2">{{ customer.name }}</td>
        <!-- 其他表格列内容 -->
      </tr>
    </tbody>
  </table>

  <!-- 分页组件,确保翻页时携带搜索参数 -->
  <Pagination :links="$page.props.customers.links" />
</template>

步骤2:后端Laravel控制器优化搜索与分页逻辑

调整控制器方法,支持动态搜索列、搜索词,同时确保分页时保留搜索参数:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Inertia\Inertia;
use App\Models\Customer;

class CustomerController extends Controller
{
    public function search(Request $request, $searchColumn = 'nr', $searchTerm = '')
    {
        // 可选:改用查询参数模式(更灵活),替换上方参数为:
        // $searchColumn = $request->query('searchColumn', 'nr');
        // $searchTerm = $request->query('searchTerm', '');

        // 验证搜索列合法性,防止非法字段导致SQL风险
        $allowedColumns = ['nr', 'name', 'email']; // 替换为你的模型实际字段
        if (!in_array($searchColumn, $allowedColumns)) {
            $searchColumn = 'nr';
        }

        // 构建查询并分页,同时携带搜索参数到分页链接
        $customers = Customer::whereLike($searchColumn, $searchTerm)
            ->paginate(10)
            ->appends([
                'searchColumn' => $searchColumn,
                'searchTerm' => $searchTerm,
            ]);

        return Inertia::render('Dashboard', [
            'customers' => $customers,
        ]);
    }
}

步骤3:确认路由配置

确保你的路由能正确接收搜索参数,示例路由:

// routes/web.php
// 路径参数模式(对应控制器上方的参数写法)
Route::get('/customer/{searchColumn}/{searchTerm}', [CustomerController::class, 'search'])->name('customer.search');

// 或查询参数模式(更推荐,URL更友好)
// Route::get('/customer', [CustomerController::class, 'search'])->name('customer.search');

关键注意点

  • 搜索时强制重置页码为1,避免出现“搜索结果只有5条,但当前在第4页”的空数据情况
  • 分页时用appends()携带搜索参数,保证翻页后搜索条件不丢失
  • 验证搜索列的合法性,防止恶意字段触发SQL注入风险
  • 使用router.visit的replace: true选项,避免浏览器历史记录中堆积大量搜索请求记录

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 15:43:17