1 month ago

Get withCount() of nested relationship

Posted 1 month ago by mallaury


Here is my problem: In my database I have departments that have lessons. Lesson belongs to many departments. So there is a pivot table named department_lesson.

Users are allow to store documents (and some other stuff) in a lesson so that documents are visible in all lessons that belong to the department.

From a list of department_lesson, I would like to display how many documents are stored in.

Here is my code:

$departmentLessons = auth()->user()->myDepartmentLessons()

In this case I get all documents in the lesson. But I just want to count them... I tried that, but it does not work of course...

$departmentLessons = auth()->user()->myDepartmentLessons()
// Other test
$departmentLessons = auth()->user()->myDepartmentLessons()

Thanks in advance !!!

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