Php Laravel 5.2:如何从一对一雄辩(hasOne)关系中检索数据
我有两种型号: 1.课程 2.费用 一门课程只有一笔费用。虽然提供输入,但当我试图访问费用数据时,它在费用栏中没有显示任何内容。我如何解决它 课程模式:Php Laravel 5.2:如何从一对一雄辩(hasOne)关系中检索数据,php,laravel,eloquent,laravel-5.2,one-to-one,Php,Laravel,Eloquent,Laravel 5.2,One To One,我有两种型号: 1.课程 2.费用 一门课程只有一笔费用。虽然提供输入,但当我试图访问费用数据时,它在费用栏中没有显示任何内容。我如何解决它 课程模式: <?php namespace App; use Illuminate\Database\Eloquent\Model; class Course extends Model { protected $table='courses'; protected $fillable = ['name']; pub
<?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');
}
}
查看页面:
<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>
课程详情
学生资料
序列号
课程名称
学费
@foreach($课程作为$行)
{{$i}
{{$row->name}
@如果($row->课程)
{{$row->课程->费用}
@恩迪夫
@endforeach
看来你把关系写错了。。。就这么做吧
请记住,课程有费用
意味着课程
是必须的,所以关系应该从课程
一侧开始,朝向费用
您的课程模式
class Course extends Model
{
protected $table='courses';
protected $fillable = ['name'];
public function fee(){
return $this->hasOne('App\Fee','course_id', 'id');
}
}
class Fee extends Model
{
protected $table='fees';
protected $fillable = ['fee','course_id'];
public function course()
{
return $this->belongsTO('App\Course', 'course_id', 'id');
}
}
您的收费模式
class Course extends Model
{
protected $table='courses';
protected $fillable = ['name'];
public function fee(){
return $this->hasOne('App\Fee','course_id', 'id');
}
}
class Fee extends Model
{
protected $table='fees';
protected $fillable = ['fee','course_id'];
public function course()
{
return $this->belongsTO('App\Course', 'course_id', 'id');
}
}
现在,您可以通过这样做获得关系。
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'));
}
公共函数列表课程(){
$course=\App\course::with('fee')->get();
//这样做是为了倾销
回声“;
print_r($course->toArray());//您将看到'fee'数组
回声“;
模具();
返回视图('listofCourse',压缩('course');
}
我不能完全理解您的情况。看起来您正在调用“课程”上的“课程”方法。我想使用课程表访问费用表数据,其中id与课程id匹配。@Andrew请解释您的问题,您可以共享数据数组吗?所以我可以指引你accordingly@Qazi谢天谢地,原来我使用了错误的关系,(我在pivot表中有一个belongdomany和额外的列)现在都排序好了。。。