2017-07-14 61 views
0

我有兩張表「耐心」和「預訂」表,並且它們之間存在「一對多」關係,我想在index_booking頁面中設置一個搜索表單,其中用戶可以在其上鍵入patient_name並自動完成根據WHERE條件顯示患者表中的所有patient_name。自動完成在laravel中找不到結果

這是預訂模式

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')) !!} 

我用的預訂控制器驗證碼使自動完成顯示來自患者表的數據 :

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'=>'[email protected]')); 
Route::get('searchajax',array('as'=>'searchajax','uses'=>'[email protected]')); 

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'); 
    } 
+1

對於SQL的LIKE運算符,Wilcards是百分號,而不是&符號。請問您能解釋一下 –

+0

嗎? –

+0

您的' - > where('patient_name','like',「&{$ request-> get('term')}&」)'行會產生SQL查詢,其中有'WHERE patient_name LIKE'&something&',它應該是'WHERE patient_name LIKE'%something%'' –

回答

0

SQL的語法與LIKE運營商匹配是:

WHERE `column` LIKE '%needle%' 

您的代碼,而另一方面,產生如下:

WHERE `column` LIKE '&needle&' 

這幾乎是一樣的,如果你輸入了:

WHERE `column` = '&needle&' 

所以你需要做的是取代&機智h %在以下行中:

->where('patient_name', 'like', "&{$request->get('term')}&") 
+0

我接受你的答案,但我不能upvote它,因爲我沒有足夠的聲譽,再次感謝你:) –

+0

沒問題,謝謝你的接受! –

+0

對不起@羅伯Robok,但是當我提交表格我收到一條錯誤消息說:病人ID字段是必需的。 –