I tried to eager load a relation:
$tournaments = Tournament::with('numCompetitors')->latest()->paginate(config('constants.PAGINATION'));
My relation in Tournament returns an integer:
public function numCompetitors()
{
return $this->competitors()->count(); // it returns 24
}
With that I get:
Call to a member function addEagerConstraints() on integer
I don't understand why is it failing.
You're doing it wrong. If you want to count relationship, use withCount()
with properly defined relationship:
Tournament::withCount('competitors')->latest()->paginate(config('constants.PAGINATION'));
If you want to count the number of results from a relationship without actually loading them you may use the withCount method, which will place a
{relation}_count
column on your resulting models.
https://laravel.com/docs/5.4/eloquent-relationships#counting-related-models
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With