Home > Net >  Method withCount does not exist
Method withCount does not exist

Time:04-12

I am using laravel 5.4 so i'm trying to count number of relations between two models "Person" and "Permanance" so this is how it is called in my controller $persons = Person::all()->withCount('Permanance')->get(); and this is the error i'm getting

(1/1) BadMethodCallException

Method withCount does not exist. in Macroable.php line 74 at Collection->__call('withCount', array('Permanance'))in PermanancesController.php line 41

CodePudding user response:

You should use directly as a static mehtod withCount without the all() method, as described here in the Laravel Docs

$posts = Post::withCount('comments')->get();

CodePudding user response:

Please call like below,

$persons = Person::withCount('Permanance')->get();

  • Related