我有两张桌子,“耐心”和“预订”表。他们之间存在“一对多”的关系。我想在index_booking页面中设置一个搜索表单,用户可以在其中键入patient_name,并根据WHERE条件自动完成从患者表中显示所有patient_name。
这是预订模式
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')) !!}
我不知道自动完成的代码语法。此代码工作正常,但在预订表中搜索,而不是在患者表中搜索</ p>
public function autoComplete(Request $request) {
$query = $request->get('term','');
$bookings=Booking::with(['patient' => function ($query) {
$query->where('company_id','=' ,Auth::user()->company_id);
}])->$data=array();
foreach ($bookings as $booking) {
$data[]=array('value'=>$booking->patient->patient_name,'id'=>$booking->patient->id);
}
if(count($data))
return $data;
else
return ['value'=>'No Result Found','id'=>''];
}
一般情况下,在尝试使用其他代码进行自动完成后,这是我的BookingController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Booking;
use App\Patient;
use App\User;
use Session;
use DB;
use Auth;
use Input;
class BookingController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$search = \Request::get('search');
$bookings = Booking::whereHas('patient', function ($query) use ($search) {
$query->where('patient_name', 'like', '%' . $search . '%');
})->where('status','=', null)->whereHas('patient', function ($query){
$query->where('company_id','=' ,Auth::user()->company_id);
})->paginate(10);
return view('booking.index')->withBookings($bookings);
我尝试使用此代码自动完成但无法正常工作。
public function autoComplete(Request $request) {
$query = $request->get('term','');
$bookings=Booking::whereHas('patient', function ($query){
$query->where('company_id','=' ,Auth::user()->company_id);
})->$data=array();
foreach ($bookings as $booking) {
$data[]=array('value'=>$booking->patient->patient_name,'id'=>$booking->id);
}
if(count($data))
return $data;
else
return ['value'=>'No Result Found','id'=>''];
}
这是路线
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>
答案 0 :(得分:0)
这样的事情应该有效:
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,
];
});
}
希望这有帮助!