如何从Laravel中的另一个表中自动完成显示数据

时间:2017-07-12 18:20:43

标签: php jquery mysql laravel autocomplete

我有两张桌子"病人"表和"预订"表,并且有一种关系"一对多"在他们之间,我想在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')) !!}

我不知道自动完成的代码语法,这段代码工作完美但是在预订表中搜索而不是在患者表中:

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);
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */

/ *我尝试使用此代码自动完成但工作* /

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'));

0 个答案:

没有答案