Lety Nava Lety Nava - 18 days ago 8
jQuery Question

Autocomplete search in Laravel

I was wondering if there is any way I can display more than one fields in the autocomplete search box. It already search for fist and last name but when the user types the last name it shows only the first name. But I wonder if there is a way that I can also display the last name or address on the search result. I am only getting the id of the guardian because it's the only thing I need to sent to the database. Here is a look at my code:

This is my function for the 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);
}


My view:

<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>


My Route:

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


I really hope you can help me. Thank you!

Answer

Why not just append the last name onto the first name?

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);
}
Comments