2016-04-15 9 views
0

Ich habe zwei Modelle: 1. Kurs. 2. Gebühr.Laravel 5.2: Wie Daten von eins zu eins eloquent abgerufen werden (hasOne) Beziehung

Ein Kurs hat nur eine Gebühr. Während der Eingabe ist es völlig in Ordnung, aber als ich versuchte, auf die Gebührendaten zuzugreifen, zeigt es nichts in der Spalte der Gebühr an. Wie löse ich es?

Course Modell:

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Course extends Model 
{ 
    protected $table='courses'; 
    protected $fillable = ['name']; 


    public function fee(){ 
     return $this->belongsTo('App\Fee'); 
    } 

} 

Fee Modell:

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Fee extends Model 
{ 
     protected $table='fees'; 
    protected $fillable = ['fee','course_id']; 
    public function course() 
    { 
     return $this->hasOne('App\Course'); 
    } 
} 

Controller:

public function listofCourse(){ 
     $course=\App\Course::all(); 
     return view('listofCourse',compact('course')); 
    } 

anzeigen Seite:

<html> 
    <head> 
     <title> Course Details </title> 

    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
    <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script> 

    </head> 
    <body> 


<div class="container"> 
<h3> Student Details </h3> 
     <table class="table table-striped table-bordered" id="example"> 
     <thead> 
      <tr> 
      <td>Serial No</td> 
      <td>Course Name</td> 
      <td>Course Fee</td> 


     </tr> 
     </thead> 
     <tbody> 
      <?php $i=1; ?> 
     @foreach($course as $row) 

      <tr> 
      <td>{{$i}}</td> 
      <td>{{$row->name }}</td> 

      <td> @if($row->course) 
        {{$row->course->fee}} 
        @endif</td> 


      </tr> 
      <?php $i++; ?> 

     @endforeach 
     </tbody> 


     </table> 

    </div> 

    </body> 
</html> 
+0

Ich bin mir nicht ganz sicher, dass ich Ihre Situation verstehe. Es sieht so aus, als ob Sie eine Methode "Kurs" auf einem "Kurs" aufrufen – tam5

+0

Ich möchte auf Gebührentabelle Daten unter Verwendung von Kursen Tabelle zugreifen, wo ID mit Kurs_ID übereinstimmt. – User57

Antwort

3

Es scheint, dass Sie die Beziehung falsch geschrieben haben ... tun Sie das einfach.

Keep in Mind, Course Has The Fee bedeutet Course Muss ist, so sollte Beziehung von Course Seite in Richtung Fee starten.

Ihr Kursmodell

class Course extends Model 
{ 
    protected $table='courses'; 
    protected $fillable = ['name']; 


    public function fee(){ 
     return $this->hasOne('App\Fee','course_id', 'id'); 
    } 

} 

Ihre Fee Modell

class Fee extends Model 
{ 
    protected $table='fees'; 
    protected $fillable = ['fee','course_id']; 
    public function course() 
    { 
     return $this->belongsTO('App\Course', 'course_id', 'id'); 
    } 
} 

Jetzt können Sie die Beziehung erhalten, indem dies zu tun.

public function listofCourse(){ 
     $course=\App\Course::with('fee')->get(); 
     // doing this for dumping purpose 
     echo "<pre>"; 
     print_r($course->toArray()); // you will see the `fee` array 
     echo "</pre>"; 
     die(); 
     return view('listofCourse',compact('course')); 
} 
+0

Danke Bruder! löste es! – User57