Home > Software design >  Complicated SQL / Laravel Eloquent Query
Complicated SQL / Laravel Eloquent Query

Time:09-23

I have a table participants where each participant belongs to a conference, which is an entry in the table conferences. Further, each participant can provide one answer which are stored in the table answers.

Now, I need a query which gets all the answers from all participants which belong to the current conference.

In particular, I would like to paginate the results, thus I am hoping for a more sophisticated approach than to simply fetch all answers and then check if they belong to a participant in this conference.

To be clear, my tables store basically the following information:

  • participants - id, conference_id
  • conferences - id
  • answers - id, participant_id

CodePudding user response:

You could use HasManyThrough. I think the example in the Laravel Doc fits perfect to your case. Laravel Doc example

CodePudding user response:

You can use hasMany and hasOne relation ship of Laravel Eloquent Model, and add more constraint when you use Eloquent Relation Ship query (As Laravel document say All Eloquent Relation Ship is query builder, so you can add constraints using where) to get results you expect.

  • Related