Laravel 5.2 : How to retrieve data from one to one eloquent (hasOne) Relationship
<p>I have two Models:
1. Course.
2. Fee.</p>

<p>One course has only one fee. While giving input it's completely ok but when I tried to access the fee data, it's showing nothing in fee's column . How do I solve it?</p>

<p>Course Model:</p>

<pre><code>&lt;?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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


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

}
</code></pre>

<p>Fee Model:</p>

<pre><code>&lt;?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-&gt;hasOne('App\Course');
    }
}
</code></pre>

<p>Controller:</p>

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

<p>View Page:</p>

<pre><code>&lt;html&gt;
    &lt;head&gt; 
        &lt;title&gt; Course Details &lt;/title&gt;

    &lt;link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"&gt;

    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"&gt;&lt;/script&gt;
    &lt;script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"&gt;&lt;/script&gt;
    &lt;script src="https://code.jquery.com/jquery-1.12.0.min.js"&gt;&lt;/script&gt; 

    &lt;/head&gt;
    &lt;body&gt;


&lt;div class="container"&gt;
&lt;h3&gt; Student Details &lt;/h3&gt;
      &lt;table  class="table table-striped table-bordered"  id="example"&gt;
        &lt;thead&gt;
          &lt;tr&gt;
            &lt;td&gt;Serial No&lt;/td&gt;
            &lt;td&gt;Course Name&lt;/td&gt;
            &lt;td&gt;Course Fee&lt;/td&gt;


        &lt;/tr&gt;
        &lt;/thead&gt;
        &lt;tbody&gt;
          &lt;?php $i=1; ?&gt;
        @foreach($course as $row)

          &lt;tr&gt;
            &lt;td&gt;{{$i}}&lt;/td&gt;
            &lt;td&gt;{{$row-&gt;name }}&lt;/td&gt;

            &lt;td&gt;    @if($row-&gt;course)
                  {{$row-&gt;course-&gt;fee}}
                   @endif&lt;/td&gt;


             &lt;/tr&gt;
          &lt;?php $i++; ?&gt;

        @endforeach
        &lt;/tbody&gt;


      &lt;/table&gt;

    &lt;/div&gt;

    &lt;/body&gt;
&lt;/html&gt;
</code></pre>