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 manyJobReview
JobReview
belongs toJob
JobReview
has manyReview
Review
belongs to manyJobReview
(?)
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 oneJobReview
JobReview
belongs toJob
JobReview
has manyJobReviewAnswer
JobReviewAnswer
belongs toJobReview
JobReviewAnswer
has oneReview
Review
has manyJobReviewAnswer
Many To Many is what you are looking for.
Review
has manyJobReview
andJobReview
has manyReview
.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 gotJobReview
.