【问题标题】:Laravel 5.2 : How to retrieve data from one to one eloquent (hasOne) RelationshipLaravel 5.2:如何从一对一的雄辩(hasOne)关系中检索数据
【发布时间】:2016-04-15 02:00:22
【问题描述】:

我有两个模型: 1. 课程。 2. 费用。

一门课程只收一笔费用。在提供输入时完全可以,但是当我尝试访问费用数据时,费用列中没有显示任何内容。我该如何解决?

课程模式:

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

控制器:

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>

【问题讨论】:

  • 我不完全确定我了解您的情况。看起来您正在“课程”上调用方法“课程”
  • 我想使用 id 与 course_id 匹配的课程表访问费用表数据。

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


【解决方案1】:

关系好像写错了……就这样吧。

记住,Course Has The Fee 表示Course 是必须的,所以关系应该从Course 一侧开始朝向Fee

您的课程模型

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

【讨论】:

  • @Andrew 请解释你的问题,你能分享你的数据阵列吗?所以我可以相应地指导你
  • @Qazi 谢谢原来我使用了错误的关系,(我在数据透视表中有一个 belongsToMany 和额外的列)现在全部排序......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-03
  • 2012-10-08
  • 2018-10-27
  • 2018-01-11
  • 2018-11-14
相关资源
最近更新 更多