Eloquent relations between three (or four?) models

41 views Asked by At

I would appreciate an input on how to properly approach the problem with relations, since I am a bit lost. I do not want to use raw queries (I would have solved this problem in no time with those), I want to use eloquent since I am still learning it.

I have three models (at the moment): Job, JobReview and Review.

  • Job has many JobReview
  • JobReview belongs to Job
  • JobReview has many Review
  • Review belongs to many JobReview (?)

To explain. I have a table with Job where N jobs can be created (not limited). Each Job can have only one JobReview, while each JobReview can have N Review. Review are basically a pre-set questions that need to be answered when rating a completed job.

I have already tried a few combinations with belongsToMany, hasMany, hasOne, but none of those are giving back results I want.

On top of all that, JobReview also needs to store two additional fields when saving Review response: rating and content. First is a number between 1-5 and second is text content.

Am I missing a fourth model here, something like:

  • Job has one JobReview
  • JobReview belongs to Job
  • JobReview has many JobReviewAnswer
  • JobReviewAnswer belongs to JobReview
  • JobReviewAnswer has one Review
  • Review has many JobReviewAnswer
1

There are 1 answers

0
Daniel Logvin On

Many To Many is what you are looking for.

Review has many JobReview and JobReview has many Review.

You will also have to work this out with a pivot table, so read the docs carefully.

I cannot fully understand your business logic, so I cannot really give a specific answer on how to do it. I don't know what your Review is since you already got JobReview.