2016-06-01 5 views
0

ich habe zwei Modelle ‚Nutzer‘ und ‚Ärzte‘ Ich habe wollen verwendet eloquent relatonahiops hasMany und belongaTo sie für Ärzte, wie untenwie „belongTo“ Beziehung in larvael verwenden

This is the model for users 
    protected $fillable = [ 
    'name', 
    'email', 
    'password', 
    'role_id', 
    'active_user', 
]; 

public function doctor() 
{ 
    return $this->hasMany('App\Doctor','user_id'); 
} 

und das Modell zu beziehen ist unter geschützt $ table = "Ärzte";

protected $fillable = [ 
    'role_id', 
    'user_id', 
    'name', 
    'date', 
    'gender', 
    'specialization', 
    'state', 
    'country', 
    'email', 
    'mobile', 
    'phone', 
    'address', 
    'password', 
    'pic' 

]; 
public function user() 
{ 
    return $this->belongsTo('User','user_id'); 
} 

jetzt habe ich eine Tabelle, wo ich die Ärzte Anzeige mit @foreach Erklärung unten

<table id="example" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%"> 
    <thead > 
    <tr > 
     <th> 
      S/N 
     </th> 
     <th> 
      Picture 
     </th> 

     <th style="text-align: center;"> 
      Name 
     </th> 

     <th style="text-align: center;"> 
      Specialization 
     </th> 

     <th style="text-align: center;"> 
      Phone Number 
     </th> 

     <th style="text-align: center;"> 
      Status 
     </th> 


     <th style="text-align: center;"> 
      Book 
     </th> 
     {{--<th style="text-align: center;">--}} 
     {{--Message--}} 
     {{--</th>--}} 

    </tr> 
    </thead> 
    <tbody> 
    <?php $c=0;?> 
    @foreach($doctors as $dr) 
     <tr style="padding: 10px; text-align: center;"> 
      <td>{{++$c}}</td> 
      <td> 
       <img src='{{url("assets/images/doctors_pic_uploads/".$dr->pic)}}' style="height:40px; width:40px;" class="img-circle"> 
      </td> 
      <td style="padding: 20px;">{{$dr->name}}</td> 
      <td style="padding: 20px; ">{{$dr->specialization}}</td> 
      <td style="padding: 20px;">{{$dr->phone}}</td> 
      <td style="padding: 20px;"> 

      </td> 
      <td style="padding: 20px;"> 
       <input type="radio" name="book" value="{{$dr->id}}"> 

      </td> 
      This is where i want to show if they are offline or online 
     </tr> 
    @endforeach 


    </tbody> 
</table> 

Mein Controller ist unter

public function BookDoctors(){ 
    if(!Auth::check()) { 
     return Redirect::to('admin/login'); 
    } 
    $opt=User::findOrFail(Auth::User()->id)->operator->first(); 
    $doctors = Doctor::all(); 
    $patients = PatientInfo::all(); 
    return view('Others.Operator.Appointment.book_doctors')->with(compact('opt','doctors','patients')); 

} 

Die Herausforderung ist jetzt, dass ich das bekommen wollen "active_user" -Feld aus der Benutzermigration und enthalten Sie das Formular, das eine Tabellenkopf von "status" hat mit "belongTo" Beziehung in Laravel

+0

Sie sollten in der Lage sein, darauf zuzugreifen, wie diese $ doctor-> user-> active_user'. Hast du es versucht? – TheFallen

+0

Versucht, dass es nicht funktioniert –

+0

Warum funktioniert es nicht? Erhalten Sie eine Fehlermeldung oder etwas? Jede Information wäre hilfreich, um es herauszufinden. – TheFallen

Antwort

0

Get Benutzerdetails in Ihrer Anfrage

$doctors = Doctor::with('user')->get(); 

Ihrer Ansicht Datei

<td>{{$dr->user->active_user}}</td> 
+0

es gibt eine Fehlermeldung zurück "Methode mit existiert nicht." –

+0

Es sollte 'Doktor sein: mit ('Benutzer') -> all();'. – TheFallen

+0

es zeigt den Fehler jetzt "Aufruf an undefinierte Methode Illuminate \ Database \ Query \ Builder :: all() in Builder.php Zeile 2306" –

Verwandte Themen