在Laravel中自动完成搜索

时间:2016-11-14 15:18:43

标签: php jquery laravel autocomplete

我想知道是否有任何方法可以在自动填充搜索框中显示多个字段。它已经搜索了名字和姓氏,但是当用户输入姓氏时,它只显示名字。但我想知道是否有一种方法可以在搜索结果中显示姓氏或地址。我只是得到了监护人的身份,因为它是我发送到数据库的唯一要求。以下是我的代码:

这是我自动完成的功能:

public function autocomplete(Request $request)
    {
        $term=$request->term;
        $data = Guardian::orWhere('first_name','LIKE','%'.$term.'%')
        ->orWhere('last_name','LIKE','%'.$term.'%')
        ->take(10)->get();
        $results=array();
        foreach($data as $key => $v)
        {
            $results[]=['id'=>$v->idguardian, 'value'=>$v->first_name];
        }
        return response()->json($results);
    }

我的观点:

  <div class="row">
    <div class="col-md-6">
      <h3>New Kid</h3>
      <p> Please search the guardian name for the kid. If the guardian isn't registered please add it</p>
      <form action="{{route('createkid')}}" method="post">
      <div class="form-group">
          <input type="text" name="searchname" class="form-control" id="searchname" placeholder="Search">
          <tr>
            <td>ID</td>
            <td><input type="text" name="idguardian" id="idguardian" class="form-control"></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
          </tr>
    </div>
        <a href="{{URL::to('addguardian/list/')}}" class="btn btn-success btn-xs" >Add Guardian</a>
      <div class ="form-group">
        <label for="first_name">First Name</label>
        <input type="text" name="first_name" class="form-control" placeholder="FirstName">
      </div>   
      <div class ="form-group">
        <label for="middle_name">Middle Name</label>
        <input type="text" name="middle_name" class="form-control" placeholder="MiddleName">
      </div>
      <div class ="form-group">
        <label for="last_name">Last Name</label>
        <input type="text" name="last_name" class="form-control" placeholder="LastName">
      </div>
        <div class ="form-group">
        <label for="dateofbirth">Date Of Birth</label>
        <input type="text" name="dateofbirth" class="form-control" placeholder="DateOfBirth">
      </div>
      <div class ="form-group">
        <label for="sex">Sex</label>
        <input type="text" name="sex" class="form-control" placeholder="Sex">
      </div>
      <div class ="form-group">
        <label for="age">Age</label>
        <input type="text" name="age" class="form-control" placeholder="Age">
      </div>
      <div class ="form-group">
        <label for="schoolname">School Name</label>
        <input type="text" name="schoolname" class="form-control" placeholder="SchoolName">
      </div>
      <div class ="form-group">
        <label for="schoolgrade">School Grade</label>
        <input type="text" name="schoolgrade" class="form-control" placeholder="SchoolGrade">
      </div>
      <div class ="form-group">
        <label for="schoolgroup">School Group</label>
        <input type="text" name="schoolgroup" class="form-control" placeholder="SchoolGroup">
      </div>
      <div class ="form-group">
        <label for="bloodtype">Blood Type</label>
        <input type="text" name="bloodtype" class="form-control" placeholder="BloodType">
      </div>
      <div class ="form-group">
        <label for="allergies">Allergies</label>
        <input type="text" name="allergies" class="form-control" placeholder="Allergies">
      </div>
      <div class ="form-group">
        <label for="enroll_date">Enroll Date</label>
        <input type="text" name="enroll_date" class="form-control" placeholder="EnrollDate">
      </div>
      <div class="form-group">
        {!! Form::label('iduser','Select User') !!}
        {!! Form::select('iduser', $kids, ['class' => 'form-control']) !!}
      </div>

      <button type="submit" class="btn btn-primary">Submit</button>
      <input type="hidden" name="_token" value="{{ Session::token()}}">
    </form>
  </div>
</div>
<script type="text/javascript">
$("#searchname").autocomplete({
  source : '{!!URL::route('autocomplete')!!}',
  minLenght:1,
  autoFocus:true,
  select:function(e, ui){
    $('#idguardian').val(ui.item.id);

  }
});
</script>

我的路线:

Route::get('/autocomplete',array('as'=>'autocomplete','uses'=>'KidsController@autocomplete'));

我真的希望你能帮助我。谢谢!

1 个答案:

答案 0 :(得分:1)

为什么不将姓氏附加到名字上?

public function autocomplete(Request $request)
{
    $term=$request->term;
    $data = Guardian::orWhere('first_name','LIKE','%'.$term.'%')
    ->orWhere('last_name','LIKE','%'.$term.'%')
    ->take(10)->get();
    $results=array();
    foreach($data as $key => $v)
    {
        $results[]=['id'=>$v->idguardian, 'value'=>$v->first_name . ' ' . $v->last_name];
    }
    return response()->json($results);
}