The same code
->relationship('department', 'name', 'parent_id'), with the parent column as the third argument. As in Select Tree, a single select chooses only the lowest level.
Note: Try it
Click Sales: it opens instead of being chosen. Open East › Boston and choose Field sales.
enableBranchNode()
The same method as in Select Tree: departments that have departments under them can be chosen too.
Note: Try it
Choose East: it is chosen, not opened.
query(), independent(false) and withCount()
->query(fn () => Department::query(), 'name', 'parent_id') builds the tree without a relationship, as in Select Tree. ->independent(false) ticks the lowest departments under a department, and ->withCount() shows how many items each department has.
Note: Try it
Open Sales and tick Canada. The four city offices under it are not chosen, only their teams: press “Show what would be saved” to see 12 departments.