Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jakegroves's avatar

Relationships only working for one entry

I am having dificulties wiith what i am doing wrong all my other relationships in other models are working perfectly fine except this one relationship. For one instance it is pulling everything correctly e.g. on my link:

http://localhost:8000/teacher/lessons/1/attendance i get the following:

{"id":4,"lesson_id":1,"attendance_id":1,"student_id":3,"attended":"true","created_at":"2016-04-12 10:49:08","updated_at":"2016-04-12 10:49:08"} {"id":5,"lesson_id":2,"attendance_id":1,"student_id":3,"attended":"true","created_at":"2016-04-12 10:49:15","updated_at":"2016-04-12 10:49:15"} {"id":10,"lesson_id":1,"attendance_id":1,"student_id":14,"attended":"true","created_at":"2016-04-14 09:23:38","updated_at":"2016-04-14 09:23:38"} {"id":12,"lesson_id":2,"attendance_id":1,"student_id":14,"attended":"true","created_at":"2016-04-14 09:23:41","updated_at":"2016-04-14 09:23:41"} {"id":13,"lesson_id":3,"attendance_id":1,"student_id":3,"attended":"true","created_at":"2016-04-14 13:44:58","updated_at":"2016-04-14 13:44:58"}

but on :

http://localhost:8000/teacher/lessons/2/attendance

I get nothing displayed.

My code is as follows:

@foreach($lesson->week as $lw)
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <td>Student</td>
                                    <td>date</td>
                                    <td>Attended</td>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($lw->students as $lws)
                                {{$lws}}
                                <tr>
                                    <td></td>
                                    <td>{{ $lw->lesson_start }}</td>
                                    <td>{{ $lws->attended}}</td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    @endforeach

my AttendanceWeek model that's using the student relationship:

class AttendanceWeek extends Model
{
    protected $table = 'attendance_week';

    protected $fillable = [
        'lesson_id', 'week_id', 'lesson_start',
    ];

    public function lesson() {
        return $this->belongsTo('App\Lesson', 'lesson_id');
    }

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

is there any reason why this would change??

edit: also when i dd($lws) it only dies and dump it for the 1st link, and not the second ... and the 'lesson' method works for them all??

0 likes
1 reply
jakegroves's avatar

forgot to define the local key .... duh ...

    public function students() {
        return $this->hasMany('App\Attendance', 'attendance_id', 'week_id');
    }

Please or to participate in this conversation.