I've got a problem I've not faced before with Eager Loading in Laravel.
I have the following table structure
- Letter
id,
sentence_id
- Sentence
id,
paragraph_id
- Paragraph
id,
book_id
- Book
id
belongsTo/belongsToMany:
letter->belongsToMany('Sentence')sentence->belongsTo('Paragraph')paragraph->belongsTo('Book')
has/hasMany:
book->hasMany('Paragraph')paragraph->hasMany('Sentence')sentence->hasMany('Letter')
This all works great. However, if I want to get all the letters in the book, I have can't seem to figure out the correct way to do this.
I've tried hasManyThrough but quickly realized that does not serve the purpose I'm trying to achieve.
I then thought I could use Eager Loading WITH hasManyThrough something to the effect of.
public function sentences(){
return $this->hasManyThrough('Sentence', 'Paragraph');
}
Which I could then do something like:
Book::sentences()->with(['letters' => function($q) use (&$letters){
$letters = $q->get()->unique();
}]);
However this does not seem to establish the relationship correctly.
What is the best way to access D through A?