2
votes

I have 2 tables attendance and student. I am trying to retrieve my stud_name from student table by using the student foreign key in attendance table. I have a controller that return the view of all the results from attendance model. I have also added the relationship in both student and attendance model but whenever i try accessing the view i got an error exception of Trying to get property 'stud_name' of non-object. Anyone able to help me?

AttendanceController

<?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')->get();
        return view('generate')->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)
    {
        //
    }
}

Student.php

<?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->belongsTo('App\Programme');
   }

   public function attendance(){
        return $this->hasMany('App\Attendance');
   }
}

Attendance.php

<?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->belongsTo('App\Student','s_id');
   }

Blade File

@foreach($attendance as $att)
    <tr>
        <td>{{$att->stud_Id->stud_name}}</td>
        //Other Data
    </tr>
@endforeach

ADDITIONAL INFOMATION

Student Table student table

Attendance Table Attendance Table

The reason all the primary key is named 'id' is because i am using voyager and it doesnt allow overriding of primary key name.

dd($attendance)

2

2 Answers

1
votes

I think you need to make changes in this line of code

public function index()
{
    $attendance = Attendance::with('student')->get();
    return view('generate')->with('attendance',$attendance);
}


@foreach($attendance as $att)
    <tr>
        <td>{{$att->student->stud_name}}</td>
        //Other Data
    </tr>
@endforeach

Attendance.php

public function student()
{
    return $this->belongsTo('App\Student','stud_id');
}

Please let me know if you have any queries.

1
votes

I think you wanna try:

@foreach($attendance as $att)
    <tr>
        <td>{{$att->student->stud_name}}</td>
        //Other Data
    </tr>
@endforeach

Also check out the docs.

In addition: I personally wouldn't call the property on the model student that holds the name stud_name. I'd call it name: the fact that it's a students name should be clear from the model name in combination with the name name.