title: Fetching Data from another table using foreign key laravel
body: <p>I have 2 tables <code>attendance</code> and <code>student</code>. I am trying to retrieve my <code>stud_name</code> from <code>student</code> table by using the <code>student</code> foreign key in <code>attendance</code> table. I have a controller that return the view of all the results from <code>attendance</code> model. I have also added the relationship in both <code>student</code> and <code>attendance</code> model but whenever i try accessing the view i got an error exception of <strong>Trying to get property 'stud_name' of non-object</strong>. Anyone able to help me?</p>
  
  <p><strong>AttendanceController</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use App\Attendance;
  
  class GenerateReportController extends Controller
  {
      /**
       * Display a listing of the resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function index()
      {
          $attendance = Attendance::with('student')-&gt;get();
          return view('generate')-&gt;with('attendance',$attendance);
      }
  
      /**
       * Show the form for creating a new resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function create()
      {
          //
      }
  
      /**
       * Store a newly created resource in storage.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return \Illuminate\Http\Response
       */
      public function store(Request $request)
      {
          //
      }
  
      /**
       * Display the specified resource.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function show($id)
      {
  
      }
  
      /**
       * Show the form for editing the specified resource.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function edit($id)
      {
          //
      }
  
      /**
       * Update the specified resource in storage.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function update(Request $request, $id)
      {
          //
      }
  
      /**
       * Remove the specified resource from storage.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function destroy($id)
      {
          //
      }
  }
  </code></pre>
  
  <p><strong>Student.php</strong> </p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Student extends Model
  {
     //Table
     protected $table = 'students';
  
     //Primary Key
     public $primaryKey = 'id';
  
     //Timestamps
     public $timestamp = true;
  
     public function programme(){
         return $this-&gt;belongsTo('App\Programme');
     }
  
     public function attendance(){
          return $this-&gt;hasMany('App\Attendance');
     }
  }
  </code></pre>
  
  <p><strong>Attendance.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Attendance extends Model
  {
     //Table
     protected $table = 'attendance';
  
     //Primary Key
     public $primaryKey = 'id';
  
     //Timestamps
     public $timestamp = true;
  
     protected $fillable = [
          'att_status','date','time',
     ];
  
     public function student()
     {
         return $this-&gt;belongsTo('App\Student','s_id');
     }
  </code></pre>
  
  <p><strong>Blade File</strong></p>
  
  <pre><code>@foreach($attendance as $att)
      &lt;tr&gt;
          &lt;td&gt;{{$att-&gt;stud_Id-&gt;stud_name}}&lt;/td&gt;
          //Other Data
      &lt;/tr&gt;
  @endforeach
  </code></pre>
  
  <p><strong>ADDITIONAL INFOMATION</strong></p>
  
  <p>Student Table
  <a href="https://i.stack.imgur.com/Wn4Gs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Wn4Gs.png" alt="student table"></a></p>
  
  <p>Attendance Table
  <a href="https://i.stack.imgur.com/IZwUr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IZwUr.png" alt="Attendance Table"></a></p>
  
  <p>The reason all the primary key is named 'id' is because i am using voyager and it doesnt allow overriding of primary key name.  </p>
  
  <p><strong>dd($attendance)</strong></p>
  
  <p><a href="https://i.stack.imgur.com/PxzAR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PxzAR.png" alt=""></a></p>