dou426098 2018-05-16 11:06
浏览 82
已采纳

PHP 500(内部服务器错误) - 遇到非数字值?

I want to have a autocomplete search input with jQuery and Laravel. But I'm getting an error when the user write at least 2 letters in the search input:

GET http://proj.test/autocomplete-search?term=ca 500 (Internal Server Error)

Do you know where is the issue? I want to show the conferences based on the letters inserted by the user in the search form.

The search input:

<div class="col col-md-6">
    <h4 class="text-white  text-center font-weight-bold">Search</h4>
    <form class="main-search">
        <input type="text" id="search" class="autocomplete dropdown-toggle" placeholder="Conference, Citiy, Category">
    </form>
</div>

Laravel routes:

Route::get('/autocomplete-search', 'AutocompleteController@search');

Laravel AutomcompleteController:

class AutocompleteController extends Controller
{
    public function search(Request $request){
        $search = $request->term;
        $conferences = Conference::where('name', 'LIKE', '%'.$search.'%')->get();

        $data= [];
        foreach ($conferences as $key => $value){
            $data[] = ['id'=>$value->id, 'value' => $value->id + " " + $value->name];
        }

        //dd($data);
        return response($data);
    }
}

jquery:

$("#search").autocomplete({
   source: "{{ URL::to('autocomplete-search') }}",
   minLength: 2,
   select:function(key, value)
   {
       console.log(value);
   }
});

EDIT: In the Network tab the error shows as:

{message: "A non-numeric value encountered", exception: "ErrorException",…}

and points to this line:

{function: "search", class: "App\Http\Controllers\AutocompleteController", type: "->"}". In "$data[] = ['id'=>$value->id, 'value' => $value->id + " " + $value->name];

  • 写回答

1条回答 默认 最新

  • douye2020 2018-05-16 11:22
    关注

    You've used the addition operator (+) when you need to use the string concatenation operator (.).

    So

    $data[] = ['id'=>$value->id, ' value' => $value->id + " " + $value->name]
    

    needs to become

    $data[] = ['id'=> $value->id, 'value' => $value->id . " " . $value->name]
    

    otherwise PHP thinks you're trying to do addition on these fields, when in fact they're text which you want to place into a single variable.

    See http://php.net/manual/en/language.operators.php for info about which operators do what in PHP.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题