php - Using a query scope in a collection laravel -


my association model looks (irrelevant code redacted):

class association extends model {     public function members() {         return $this->hasmany('app\member');     }  } 

my member model looks this:

class member extends model {     public function scopeactive($query) {         return $query->where('membership_ended_at', null);     }     public function scopeinactive($query) {         return $query->wherenotnull('membership_ended_at');     } } 

this want able do:

$association = association::find(49); $association->members->active()->count(); 

now, i'm aware there's difference between query , collection. i'm asking if there's kind of similar scope collections. of course, optimal solution not have write 2 active methods, use 1 both purposes.

(question answered in comments, minus write proper answer)

it not possible use query scope in colletion, since query scope concept used in eloquent add constraints database query while collections collection of things (data, objects, etc).

in case, need change line:

$association->members->active()->count(); 

to:

$association->members()->active()->count(); 

this works because when call members method, getting querybuilder instance, , can start chaining scopes query before calling count method.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -