I have an application where users can control their properties and leases.
These are the relationships defined:
//Team.php
/**
* A team can have many properties.
*/
public function properties():
{
return $this->hasMany(Property::class);
}
//Property.php
/**
* A property can have many leases.
*/
public function leases():
{
return $this->hasMany(Lease::class);
}
As you can see here, a Team can have many properties, and each property can also have many leases.
I am trying to figure out how I can get the number of leases that is associated with a Team:
$team = Auth::user()->currentTeam;
return $team->properties->leases->count();
However, the above code returns an error:
Property [leases] does not exist on this collection instance.
properties; that's a Collection ofPropertyinstances, so calling->properties->leaseswon't work as it doesn't know whichproperty->leasesyou're referencing. There's ahasManyThrough(), but it doesn't work for nestedmany-to-many, buthasManyDepp(), via this package does.hasManyThroughI do believe I can use it actually. (See my proposed answer)many-to-manylike I originally thought, you have nestedone-to-many(one Team -> many Properties -> many Leases), sohasManyThroughshould actually work just fine here. Glad you got it working regardless!