The issue you're encountering is due to the type hinting in the load method, which expects an instance of Illuminate\Database\Eloquent\Collection. However, your custom SiteCollection extends Illuminate\Support\Collection, not Illuminate\Database\Eloquent\Collection.
To resolve this, you should extend Illuminate\Database\Eloquent\Collection instead of Illuminate\Support\Collection in your SiteCollection class. Here's how you can modify your SiteCollection class:
namespace App\Support;
use Illuminate\Database\Eloquent\Collection;
class SiteCollection extends Collection
{
// ...
}
By extending Illuminate\Database\Eloquent\Collection, your custom collection will be compatible with Eloquent's expectations, and the load method should work as intended.
Here's the updated SiteCollection class:
namespace App\Support;
use Illuminate\Database\Eloquent\Collection;
class SiteCollection extends Collection
{
// Custom methods and properties for your SiteCollection
}
With this change, your Site model's newCollection method will return an instance of SiteCollection that is compatible with Eloquent's lazy eager loading.
Your Site model remains the same:
use Illuminate\Database\Eloquent\Model;
use App\Support\SiteCollection;
class Site extends Model
{
/**
* Create a new Eloquent Collection instance.
*
* @param array<int, \Illuminate\Database\Eloquent\Model> $models
* @return SiteCollection<int, \Illuminate\Database\Eloquent\Model>
*/
public function newCollection(array $models = []): SiteCollection
{
return new SiteCollection($models);
}
}
This should resolve the error you're encountering when trying to lazy eager load the sites relationship.