我有两个表“patient”和“booking”表,并且它们之间存在“一对多”关系,我想在index_booking页面中设置一个搜索表单,用户可以在其中键入patient_name并根据WHERE条件自动完成显示患者表中的所有患者名称。
这是预订模式
class Booking extends Eloquent
{
public function patient()
{
return $this->belongsTo('App\Patient');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
这是患者模型
class Patient extends Eloquent
{
public function booking()
{
return $this->hasMany('App\Booking');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
我在预订的索引页面中使用了这段代码
{!! Form::text('search_text', null, array('placeholder' => 'Search Text','class' => 'form-control','id'=>'search_text')) !!}
我在Booking Controller中使用此代码进行自动填充以显示数据 来自病人表:
public function autoComplete(Request $request)
{
$patients = Patient::where('company_id', Auth::user()->company_id)
->where('patient_name', 'like', "&{$request->get('term')}&")
->get();
if ($patients->isEmpty()) {
return ['value' => 'No Result Found', 'id' => ''];
}
return $patients->map(function ($patient) {
return [
'id' => $patient->id,
'value' => $patient->patient_name,
];
});
}
这是路线
Route::get('autocomplete',array('as'=>'autocomplete','uses'=>'BookingController@index'));
Route::get('searchajax',array('as'=>'searchajax','uses'=>'BookingController@autoComplete'));
Javascript代码
<script >
$(document).ready(function() {
src = "{{ route('searchajax') }}";
$("#search_text").autocomplete({
source: function(request, response) {
$.ajax({
url: src,
dataType: "json",
data: {
term : request.term
},
success: function(data) {
response(data);
}
});
},
minLength: 3,
});
});
</script>
当我在搜索框中输入任何患者姓名时,我收到了一条未找到结果的消息
这是预约控制器中的验证器:
public function store(Request $request)
{
//Validate Data
$this->validate($request, [
'patient_id'=> 'required|integer',
'booking_date'=> 'required|max:255',
'tybe'=> 'required',
'value'=>'required',
'doctor_name',
'patient_history',
'pharma',
'complaint',
'diagnosis',
'recomind',
'prescription',
'notes',
'document',
'by',
]);
//Insert Data to Database
$booking = new Booking;
$booking->patient_id = $request->patient_id;
$booking->booking_date = $request->booking_date;
$booking->tybe = $request->tybe;
$booking->value = $request->value;
$booking->doctor_name = $request->doctor_name;
$booking->patient_history = $request->patient_history;
$booking->pharma = $request->pharma;
$booking->complaint = $request->complaint;
$booking->diagnosis = $request->diagnosis;
$booking->recomind = $request->recomind;
$booking->prescription = $request->prescription;
$booking->notes = $request->notes;
$booking->document = $request->document;
$booking->by = $request->by;
$booking->save();
//to save multi selection Tags ,dont foget to add [] after -> tags in create post page then write this code here
//$post->tags()->sync($request->tags, false);
//Show Flash Message
Session::flash('success','تم حفظ البياانات');
//Redirect to another Page
return redirect()->route('booking.index');
}
答案 0 :(得分:0)
SQL与LIKE
运算符匹配的语法是:
WHERE `column` LIKE '%needle%'
另一方面,您的代码产生以下内容:
WHERE `column` LIKE '&needle&'
这与您输入的内容几乎相同:
WHERE `column` = '&needle&'
因此,您需要做的是将&
替换为以下行中的%
:
->where('patient_name', 'like', "&{$request->get('term')}&")