php - 拉维尔 5.2 : How to retrieve data from one to one eloquent (hasOne) Relationship

标签 php laravel eloquent laravel-5.2 one-to-one

我有两个模型: 1.类(class)。 2.费用。

一门类(class)只有一次费用。虽然提供输入是完全可以的,但是当我尝试访问费用数据时,它在费用栏中没有显示任何内容。我该如何解决?

类(class)模型:

<?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');
    }

}

收费模式:

<?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'));
    }

查看页面:

<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>

最佳答案

看来你把关系写错了……就这样吧。

请记住,Course Has The Fee 意味着 Course 是必须的,所以关系应该从 Course 开始到 Fee.

您的类(class)模型

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'));
}

关于php - 拉维尔 5.2 : How to retrieve data from one to one eloquent (hasOne) Relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36637037/

相关文章:

mysql - 在 Laravel Eloquent 中使用 Levenshtein 函数?

php - 如何在不同浏览器的手机上自动修复网站的分辨率?

php - 列计数与正确语法中的行错误处的值计数不匹配

php - 在 MySql 中创建具有许多外键的大表

php - 查询范围如何在 Laravel 中工作?

php - 查找前 5 条记录的 Eloquent 查询

php - Amazon Webservice 上的 mySQL RDS 实例性能

javascript - 将 JSON 数据从 php 传递到 html-data 属性,然后传递到 Javascript

laravel - 需要将数组传递给路由和 Controller

css - 使 Bootstrap 卡占用空白空间