Chev
Chev

Reputation: 43

Laravel 5.6 - 3 level relationship

I have the following Models:

These are their relationships:

Tables:

I'm currently doing this, but want to get/show the grade name too of the upload, but not sure how?:

@foreach ($assessment->uploads as $upload)
  {{ $upload }}
@endforeach

Upvotes: 1

Views: 225

Answers (2)

C. Ferrante
C. Ferrante

Reputation: 11

Anyone struggling with converting legacy code to Laravel...when you need to join tables on multiple columns, I highly recommend using https://github.com/topclaudy/compoships!

I ran into this excellent code while reading an article here: Laravel Eloquent: multiple foreign keys for relationship

Upvotes: 1

afsal c
afsal c

Reputation: 610

I edited your question.

Make relation like i edited .

  • Assessment hasMany Upload
  • Upload belongsTo Assessment
  • Upload belongsTo Grade
  • Grade hasMany Upload

And then try this

@foreach ($assessment->uploads as $upload)
 {{ $upload->grade->name }}
@endforeach

Upvotes: 2

Related Questions