1
votes

I have an eloquent query which returns a with a hasMany relationship. With that relationship, I now need to display data from another relationship. Is this possible with a single query?

My Eloquent query fetching with a relationship;

$tickets = Tickets::with('ticketQuestions')->where('event_id', $id)->get();

My TicketQuestions model also has the following relationship;

public function questions() {
    return $this->belongsTo(Questions::class);
}

Is it possible to fetch the questions from the Questions model as well?

So that my page can display the tickets, the ticket questions (look up table), and the questions themselves.

1

1 Answers

5
votes

You can do so by using the dot notation:

$tickets = Tickets::with('ticketQuestions.questions')->where('event_id', $id)->get();

Is that what you're looking for?