未捕获的类型错误:b.toLowerCase不是一个函数



我正在获取一些记录:

$companies = AppUser::where('type', 'pet-salon')->orWhere('type', 'veterinarian')->get();
return response()->json($companies);

返回的数据是一个对象数组:

[{
  id: 2, 
  type: "xxx", 
  deactivate: 0, 
  xxx: "Hello", 
  middle_name: "Mid",   
  lastname: "xxx",
  //...
}]

这是 jQuery 类型预行代码:

$('#getCompaniesForConnection').typeahead({
  source:  function (query, process) {
    return $.get('/all/companies', { query: query }, function (data) {
      return process(data);
    });
  }
});

它给我的例外:

未捕获的类型错误:b.toLowerCase 不是函数

并且结果下拉列表也没有显示,我在这里错过了什么?

是的,你需要json_encode你的公司。

$companies = AppUser::where('type', 'pet-salon')->orWhere('type', 
'veterinarian')->get();
$result = json_encode($companies ); // return this or echo

首先,PHP 代码看起来像是一个 laravel 代码,所以你可以像这样返回 $companies 变量:

$companies = AppUser::where('type', 'pet-salon')->orWhere('type', 'veterinarian')->get();
return $companies;

由于模型和集合在转换为字符串时会转换为 JSON,因此可以直接从应用程序的路由或控制器返回 Eloquent 对象。而且,让我们看看process函数的定义,以确保这不是错误的来源。

最新更新