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>
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
Ich möchte auf Gebührentabelle Daten unter Verwendung von Kursen Tabelle zugreifen, wo ID mit Kurs_ID übereinstimmt. – User57