title: How to make autocomplete show data from another table in Laravel
body: <p>i have tow table "patient" table and "booking" table , and there is a relationship "One to Many" between them ,i want to set up a search form in index_booking page where user can type a patient_name on it and auto complete show all patient_name from patient table according to WHERE Condition.</p>
  
  <p>this is Booking Model:</p>
  
  <pre><code>class Booking extends Eloquent
  {
  
   public function patient()
      {
      return $this-&gt;belongsTo('App\Patient'); 
  
      }
  
      public function user()
      {
      return $this-&gt;belongsTo('App\User'); 
  
      }
  }
  </code></pre>
  
  <p>this is Patient Model:</p>
  
  <pre><code>class Patient extends Eloquent
  {
       public function booking()
      {
      return $this-&gt;hasMany('App\Booking'); 
  
      }
  
       public function user()
      {
      return $this-&gt;belongsTo('App\User'); 
  
      }
  }
  </code></pre>
  
  <p>and i used this code in index page of booking:</p>
  
  <pre><code>{!! Form::text('search_text', null, array('placeholder' =&gt; 'Search Text','class' =&gt; 'form-control','id'=&gt;'search_text')) !!}
  </code></pre>
  
  <p>i dont know the syntax of code to auto complete, this code code working perfect but to search in booking table not in patient table:</p>
  
  <p>public function autoComplete(Request $request) {
              $query = $request->get('term','');</p>
  
  <pre><code>        $bookings=Booking::with(['patient' =&gt; function ($query) {
          $query-&gt;where('company_id','=' ,Auth::user()-&gt;company_id);
  
         }])-&gt;$data=array();
          foreach ($bookings as $booking) {
                  $data[]=array('value'=&gt;$booking-&gt;patient-&gt;patient_name,'id'=&gt;$booking-&gt;patient-&gt;id);
          }
          if(count($data))
               return $data;
          else
              return ['value'=&gt;'No Result Found','id'=&gt;''];
      }
  </code></pre>
  
  <p>in general this is my BookingController after trying another code for auto complete:</p>
  
  <pre><code>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-&gt;middleware('auth');
      }
      /**
       * Display a listing of the resource.
       *
       * @return \Illuminate\Http\Response
       */
  </code></pre>
  
  <p>/* this code to make normal search box by type patient name and its working grate */</p>
  
  <pre><code>    public function index()
      {
  
  
  $search = \Request::get('search');
  
  $bookings = Booking::whereHas('patient', function ($query) use ($search) {
       $query-&gt;where('patient_name', 'like', '%' . $search . '%');
  })-&gt;where('status','=', null)-&gt;whereHas('patient', function ($query){
    $query-&gt;where('company_id','=' ,Auth::user()-&gt;company_id); 
  })-&gt;paginate(10);
  
  
  
  
           return view('booking.index')-&gt;withBookings($bookings);
      /**
       * Show the form for creating a new resource.
       *
       * @return \Illuminate\Http\Response
       */
  </code></pre>
  
  <p>/*i tried this code to autocomplete but working */  </p>
  
  <pre><code>public function autoComplete(Request $request) {
          $query = $request-&gt;get('term','');
  
          $bookings=Booking::whereHas('patient', function ($query){
          $query-&gt;where('company_id','=' ,Auth::user()-&gt;company_id);
  
          })-&gt;$data=array();
          foreach ($bookings as $booking) {
                  $data[]=array('value'=&gt;$booking-&gt;patient-&gt;patient_name,'id'=&gt;$booking-&gt;id);
          }
          if(count($data))
               return $data;
          else
              return ['value'=&gt;'No Result Found','id'=&gt;''];
      }
  </code></pre>
  
  <p>and this is Route:</p>
  
  <pre><code>Route::get('autocomplete',array('as'=&gt;'autocomplete','uses'=&gt;'BookingController@index'));
  Route::get('searchajax',array('as'=&gt;'searchajax','uses'=&gt;'BookingController@autoComplete'));
  </code></pre>