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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题