如何使用外键进行Eloquent搜索查询

时间:2017-06-30 14:51:04

标签: mysql laravel search eloquent

我正在尝试通过搜索列(patient_id)在(预订)表中创建搜索框,此列是(患者)表的外键,此列(patient_id)通过foreach获取(patient_name)的数据{{$ booking-> patient-> patient_name}},问题是我不知道如何在我的控制器(BookingController)中通过(patient_id)搜索(patient_name),任何帮助?对不起我的语言:(

这是我的预订控制器:

public function index()
{
    $search = \Request::get('search');

     $bookings = Booking::with('patient')->where('status','=', null)-
>where('patient_name','like', '%'. $search. '%' )->paginate(10);

     return view('booking.index')->withBookings($bookings);
}

我的预订模式:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Eloquent;

class Booking extends Eloquent
{

public function patient()
{
return $this->belongsTo('App\Patient'); 

}
} 

我的患者模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Eloquent;
class Patient extends Eloquent
{
 public function booking()
{
return $this->hasMany('App\Booking'); 

}
}

更新..这是索引页

    {!! Form::open(['method'=>'GET','url'=>'booking','class'=>'navbar-form navbar-left','role'=>'search'])  !!}
<a href="{{ url('booking.index') }}" class="btn btn-primary btn-sm"><span class="glyphicon glyphicon-plus"></span> Add</a>

<div class="input-group custom-search-form">
    <input type="text" class="form-control" name="search" placeholder="Search...">
    <span class="input-group-btn">
        <button class="btn btn-default-sm" type="submit">
            <i class="fa fa-search"> <span class="hiddenGrammarError" pre="" data-mce-bogus="1"></span></i>
        </button>
    </span>
</div>
{!! Form::close() !!} 
           <th>حجز باسم</th>
                        <th>القيمة</th>
                        <th>اسم الطبيب</th>
                        <th>النوع</th>
                    </tr> 
                  </thead>
                  <div ng-controller="myController">
                  <tbody>
                    @foreach ($bookings as $booking)
                          <tr ng-repeat="booking in bookings|filter:searching">
                            <td align="center">
                              <a href="{{route('booking.edit',$booking->id)}}" class="btn btn-default"><em class="fa fa-pencil"></em></a>
                              <a class="btn btn-danger"><em class="fa fa-trash"></em></a>
                            </td>
                            <td class="hidden-xs">{{$booking->id }}</td>


                 <th>{{$booking->booking_date }}</th>
                 <th>{{$booking->patient->patient_name }}</th>
                 <td>{{$booking->value }}</td>
                 <td>{{$booking->doctor_name }}</td>
                 <td>{{$booking->tybe }}</td>
                          </tr>
                @endforeach          
                        </tbody>
                </table>

              </div>
              <!--
              <div class="panel-footer">
                <div class="row">
                  <div class="col col-xs-4">Page 1 of 5
                  </div>
                  <div class="col col-xs-8">
                    <ul class="pagination hidden-xs pull-right">
                      <li><a href="#">1</a></li>
                      <li><a href="#">2</a></li>
                      <li><a href="#">3</a></li>
                      <li><a href="#">4</a></li>
                      <li><a href="#">5</a></li>
                    </ul>
                    <ul class="pagination visible-xs pull-right">
                        <li><a href="#">«</a></li>
                        <li><a href="#">»</a></li>
                    </ul>
                  </div>
                </div>
              </div>
            -->
            </div>
          </table>
<div class="text-center">
            {!! $bookings->links(); !!}
       </div>
</div></div></div>  

3 个答案:

答案 0 :(得分:1)

Eager Loading

$bookings = Booking::with(['patient' => function($query) use ($search){
     $query->where('patient_name', 'like', '%' . $search . '%');
}])->where('status','=', null)->paginate(10);

如果您不确定将患者分配给所有预订,那么您应该使用以下查询:

$bookings = Booking::whereHas('patient', function ($query) use ($search) {
     $query->where('patient_name', 'like', '%' . $search . '%');
})->where('status','=', null)->paginate(10);

答案 1 :(得分:1)

public function index(Request $request)
{
    $keyword = $request->input('searchquery');
    $bookings= Booking::whereHas('patient', function($query) use ($keyword) {$query->where('patient_name', 'like', $keyword);})->get();
    return view('booking.index',compact('bookings'));
}

答案 2 :(得分:-1)

尝试:

C:\Users\Jahidul>npm install -g angular-cli npm WARN registry Using stale data from http://registry.npmjs.org/ because the h ost is inaccessible -- are you offline? npm WARN deprecated angular-cli@1.0.0-beta.28.3: angular-cli has been renamed to @angular/cli. Please update your dependencies. npm WARN deprecated minimatch@2.0.10: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue npm ERR! code ENOTFOUND npm ERR! errno ENOTFOUND npm ERR! network request to http://registry.npmjs.org/micromatch failed, reason: getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:8080 npm ERR! network This is a problem related to network connectivity. npm ERR! network In most cases you are behind a proxy or have bad network settin gs. npm ERR! network npm ERR! network If you are behind a proxy, please make sure that the npm ERR! network 'proxy' config is set properly. See: 'npm help config' npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\Jahidul\AppData\Roaming\npm-cache\_logs\2017-06-30T14_57_5 6_218Z-debug.log

如果您从某些输入中搜索,请尝试使用:

dd($search);