1
votes

I have the following Models:

  • Assessment
  • Upload
  • Grade

These are their relationships:

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

Tables:

  • users
  • assessment (id, user_id, name)
  • uploads (id, assessment_id, grade_id, filename, size)
  • grade (id, name)

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
2

2 Answers

2
votes

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
1
votes

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