-
Notifications
You must be signed in to change notification settings - Fork 29
Scopes
Eustáquio Rangel edited this page Aug 5, 2013
·
5 revisions
Previous: Relations - Next: Factories
Scopes are easier ways to remember and execute some well-defined query behaviours. They are created using the scope name
and one of the two ways explained below.
We can create scopes sending an array with the conditions, like:
User::scope("first_level",array("level"=>1));
foreach(User::first_level() as $user) {
}
We can create scopes sending a callable
(anonymous function, closure), like:
User::scope("by_level",function($args) { return "level=".$args[0]; });
echo "There are ".User::by_level(1)->count()," users on the first level");
User::scope("by_level_and_date",function($args) { return "level=".$args[0]." and created_at<'".$args[1]." 23 :59:59'"; });
echo "There are ".User::by_level_and_date(2,date("Y-m-d"))->count()." users on the second level, created before today";