Mandrillcomics
4 months ago

Complex Eloquent / SQL request, need backup

Posted 4 months ago by Mandrillcomics

Hello,

I'm getting stuck on a request that I think could be really simple but I can't find a way to do it.

I have a list of authors with books with pages.

I need to get the list of Authors with books with at least one page published.

The twist is there can be more than one author by book.

in user.php public function author() { return $this->BelongsToMany(Book::class, user_book', 'user_id', 'book_id'); }

in book.php, I've got an attribute with the total pages :

public function pages()
{
    return $this->hasMany(Page::class);
}

public function gettotalpagesAttribute() { return $this->pages()->where('published', 1)->count(); }

In my controller : $authors = User::whereHas('author', function($q) { $q->pages()->where('published', 1); })->get();

I know I can't call pages() here, but I have no clue on how to get it.

Please sign in or create an account to participate in this conversation.