{"action":"created","comment":{"url":"https://api.github.com/repos/laravel/framework/pulls/comments/46379115","id":46379115,"diff_hunk":"@@ -335,18 +337,31 @@ public static function clearBootedModels()\n     /**\n      * Register a new global scope on the model.\n      *\n-     * @param  \\Illuminate\\Database\\Eloquent\\ScopeInterface  $scope\n+     * @param  \\Illuminate\\Database\\Eloquent\\ScopeInterface|\\Closure|string  $scope\n+     * @param  \\Closure|null  $implementation\n      * @return void\n      */\n-    public static function addGlobalScope(ScopeInterface $scope)\n+    public static function addGlobalScope($scope, Closure $implementation = null)\n     {\n-        static::$globalScopes[get_called_class()][get_class($scope)] = $scope;\n+        if (is_string($scope) && $implementation) {","path":"src/Illuminate/Database/Eloquent/Model.php","position":29,"original_position":29,"commit_id":"b99a35a39182c0be1b2a0ddce06daac04008ee4c","original_commit_id":"b99a35a39182c0be1b2a0ddce06daac04008ee4c","user":{"login":"lucasmichot","id":513603,"avatar_url":"https://avatars.githubusercontent.com/u/513603?v=3","gravatar_id":"","url":"https://api.github.com/users/lucasmichot","html_url":"https://github.com/lucasmichot","followers_url":"https://api.github.com/users/lucasmichot/followers","following_url":"https://api.github.com/users/lucasmichot/following{/other_user}","gists_url":"https://api.github.com/users/lucasmichot/gists{/gist_id}","starred_url":"https://api.github.com/users/lucasmichot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lucasmichot/subscriptions","organizations_url":"https://api.github.com/users/lucasmichot/orgs","repos_url":"https://api.github.com/users/lucasmichot/repos","events_url":"https://api.github.com/users/lucasmichot/events{/privacy}","received_events_url":"https://api.github.com/users/lucasmichot/received_events","type":"User","site_admin":false},"body":"We usually do:\r\n`if (is_string($scope) && $implementation !== null) {`","created_at":"2015-12-02T05:43:34Z","updated_at":"2015-12-02T05:43:34Z","html_url":"https://github.com/laravel/framework/pull/11069#discussion_r46379115","pull_request_url":"https://api.github.com/repos/laravel/framework/pulls/11069","_links":{"self":{"href":"https://api.github.com/repos/laravel/framework/pulls/comments/46379115"},"html":{"href":"https://github.com/laravel/framework/pull/11069#discussion_r46379115"},"pull_request":{"href":"https://api.github.com/repos/laravel/framework/pulls/11069"}}},"pull_request":{"url":"https://api.github.com/repos/laravel/framework/pulls/11069","id":51705470,"html_url":"https://github.com/laravel/framework/pull/11069","diff_url":"https://github.com/laravel/framework/pull/11069.diff","patch_url":"https://github.com/laravel/framework/pull/11069.patch","issue_url":"https://api.github.com/repos/laravel/framework/issues/11069","number":11069,"state":"open","locked":false,"title":"[5.2] Simplify global scopes","user":{"login":"acasar","id":6329543,"avatar_url":"https://avatars.githubusercontent.com/u/6329543?v=3","gravatar_id":"","url":"https://api.github.com/users/acasar","html_url":"https://github.com/acasar","followers_url":"https://api.github.com/users/acasar/followers","following_url":"https://api.github.com/users/acasar/following{/other_user}","gists_url":"https://api.github.com/users/acasar/gists{/gist_id}","starred_url":"https://api.github.com/users/acasar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/acasar/subscriptions","organizations_url":"https://api.github.com/users/acasar/orgs","repos_url":"https://api.github.com/users/acasar/repos","events_url":"https://api.github.com/users/acasar/events{/privacy}","received_events_url":"https://api.github.com/users/acasar/received_events","type":"User","site_admin":false},"body":"This PR proposes a simplified way of dealing with global scopes in Eloquent. Right now, even for the simplest global scopes (like ``is_active = 1``) we need to create a new class that implements ``ScopeInterface`` and we need to implement ``apply`` and ``remove`` methods (and especially ``remove`` can be tricky).\r\n\r\nThis PR makes the following changes:\r\n\r\n- scopes are applied just-in-time in the Eloquent Builder rather than in the Model, so that they can be removed very easily before they are even added to the query. Therefore the ``remove`` method in the ``ScopeInterface`` is not needed any more. It was also removed from the contract.\r\n- The ``applyGlobalScope`` method on the Model now also accepts closures to enable an easy way of adding simple global scopes. ``ScopeInterface`` implementations are of course still accepted to maximize BC.\r\n-  ``applyGlobalScope`` now also accepts an optional identifier as the first argument which can serve as a key to remove the scope by.\r\n\r\nSome examples:\r\n```php\r\nclass User extends Model\r\n{\r\n    public static function boot()\r\n    {\r\n        static::applyGlobalScope(function($builder) {\r\n            $builder->where('is_active', 1);\r\n        });\r\n        parent::boot();\r\n    }\r\n}\r\n\r\nUser::all(); // SELECT * FROM users WHERE is_active=1\r\nUser::removeGlobalScopes()->get(); // SELECT * FROM users\r\n```\r\n\r\n```php\r\nclass User extends Model\r\n{\r\n    public static function boot()\r\n    {\r\n        static::applyGlobalScope('active', function($builder) {\r\n            $builder->where('is_active', 1);\r\n        });\r\n\r\n        static::applyGlobalScope('order', function($builder) {\r\n            $builder->orderBy('name');\r\n        });\r\n\r\n       parent::boot();\r\n    }\r\n}\r\n\r\nUser::all(); // SELECT * FROM users WHERE is_active=1 ORDER BY name ASC\r\nUser::removeGlobalScope('active')->get(); // SELECT * FROM users ORDER BY name ASC\r\n```\r\n\r\nAgain, I tried to avoid BC breaks as much as possible, so the old way of using ``ScopeInterface`` still works. Only the ``remove`` method can now be deleted.\r\n\r\nAny comments? :) Do you all think it's worth to continue working on this?\r\n\r\nEdit: updated to reflect the latest changes\r\nEdit2: updated with removeGlobalScopes() example","created_at":"2015-11-24T19:29:32Z","updated_at":"2015-12-02T05:43:34Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e034ffad0e00c1f90f1ec3f2b4761c80c9a244a6","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/laravel/framework/pulls/11069/commits","review_comments_url":"https://api.github.com/repos/laravel/framework/pulls/11069/comments","review_comment_url":"https://api.github.com/repos/laravel/framework/pulls/comments{/number}","comments_url":"https://api.github.com/repos/laravel/framework/issues/11069/comments","statuses_url":"https://api.github.com/repos/laravel/framework/statuses/b99a35a39182c0be1b2a0ddce06daac04008ee4c","head":{"label":"acasar:scopes","ref":"scopes","sha":"b99a35a39182c0be1b2a0ddce06daac04008ee4c","user":{"login":"acasar","id":6329543,"avatar_url":"https://avatars.githubusercontent.com/u/6329543?v=3","gravatar_id":"","url":"https://api.github.com/users/acasar","html_url":"https://github.com/acasar","followers_url":"https://api.github.com/users/acasar/followers","following_url":"https://api.github.com/users/acasar/following{/other_user}","gists_url":"https://api.github.com/users/acasar/gists{/gist_id}","starred_url":"https://api.github.com/users/acasar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/acasar/subscriptions","organizations_url":"https://api.github.com/users/acasar/orgs","repos_url":"https://api.github.com/users/acasar/repos","events_url":"https://api.github.com/users/acasar/events{/privacy}","received_events_url":"https://api.github.com/users/acasar/received_events","type":"User","site_admin":false},"repo":{"id":46793184,"name":"framework","full_name":"acasar/framework","owner":{"login":"acasar","id":6329543,"avatar_url":"https://avatars.githubusercontent.com/u/6329543?v=3","gravatar_id":"","url":"https://api.github.com/users/acasar","html_url":"https://github.com/acasar","followers_url":"https://api.github.com/users/acasar/followers","following_url":"https://api.github.com/users/acasar/following{/other_user}","gists_url":"https://api.github.com/users/acasar/gists{/gist_id}","starred_url":"https://api.github.com/users/acasar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/acasar/subscriptions","organizations_url":"https://api.github.com/users/acasar/orgs","repos_url":"https://api.github.com/users/acasar/repos","events_url":"https://api.github.com/users/acasar/events{/privacy}","received_events_url":"https://api.github.com/users/acasar/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/acasar/framework","description":"","fork":true,"url":"https://api.github.com/repos/acasar/framework","forks_url":"https://api.github.com/repos/acasar/framework/forks","keys_url":"https://api.github.com/repos/acasar/framework/keys{/key_id}","collaborators_url":"https://api.github.com/repos/acasar/framework/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/acasar/framework/teams","hooks_url":"https://api.github.com/repos/acasar/framework/hooks","issue_events_url":"https://api.github.com/repos/acasar/framework/issues/events{/number}","events_url":"https://api.github.com/repos/acasar/framework/events","assignees_url":"https://api.github.com/repos/acasar/framework/assignees{/user}","branches_url":"https://api.github.com/repos/acasar/framework/branches{/branch}","tags_url":"https://api.github.com/repos/acasar/framework/tags","blobs_url":"https://api.github.com/repos/acasar/framework/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/acasar/framework/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/acasar/framework/git/refs{/sha}","trees_url":"https://api.github.com/repos/acasar/framework/git/trees{/sha}","statuses_url":"https://api.github.com/repos/acasar/framework/statuses/{sha}","languages_url":"https://api.github.com/repos/acasar/framework/languages","stargazers_url":"https://api.github.com/repos/acasar/framework/stargazers","contributors_url":"https://api.github.com/repos/acasar/framework/contributors","subscribers_url":"https://api.github.com/repos/acasar/framework/subscribers","subscription_url":"https://api.github.com/repos/acasar/framework/subscription","commits_url":"https://api.github.com/repos/acasar/framework/commits{/sha}","git_commits_url":"https://api.github.com/repos/acasar/framework/git/commits{/sha}","comments_url":"https://api.github.com/repos/acasar/framework/comments{/number}","issue_comment_url":"https://api.github.com/repos/acasar/framework/issues/comments{/number}","contents_url":"https://api.github.com/repos/acasar/framework/contents/{+path}","compare_url":"https://api.github.com/repos/acasar/framework/compare/{base}...{head}","merges_url":"https://api.github.com/repos/acasar/framework/merges","archive_url":"https://api.github.com/repos/acasar/framework/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/acasar/framework/downloads","issues_url":"https://api.github.com/repos/acasar/framework/issues{/number}","pulls_url":"https://api.github.com/repos/acasar/framework/pulls{/number}","milestones_url":"https://api.github.com/repos/acasar/framework/milestones{/number}","notifications_url":"https://api.github.com/repos/acasar/framework/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/acasar/framework/labels{/name}","releases_url":"https://api.github.com/repos/acasar/framework/releases{/id}","created_at":"2015-11-24T13:25:23Z","updated_at":"2015-11-24T13:25:29Z","pushed_at":"2015-11-26T09:45:17Z","git_url":"git://github.com/acasar/framework.git","ssh_url":"git@github.com:acasar/framework.git","clone_url":"https://github.com/acasar/framework.git","svn_url":"https://github.com/acasar/framework","homepage":"http://laravel.com","size":19499,"stargazers_count":0,"watchers_count":0,"language":"PHP","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"5.1"}},"base":{"label":"laravel:5.2","ref":"5.2","sha":"9158033560c2a58ebf9eb2add76734cceee9cbd9","user":{"login":"laravel","id":958072,"avatar_url":"https://avatars.githubusercontent.com/u/958072?v=3","gravatar_id":"","url":"https://api.github.com/users/laravel","html_url":"https://github.com/laravel","followers_url":"https://api.github.com/users/laravel/followers","following_url":"https://api.github.com/users/laravel/following{/other_user}","gists_url":"https://api.github.com/users/laravel/gists{/gist_id}","starred_url":"https://api.github.com/users/laravel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/laravel/subscriptions","organizations_url":"https://api.github.com/users/laravel/orgs","repos_url":"https://api.github.com/users/laravel/repos","events_url":"https://api.github.com/users/laravel/events{/privacy}","received_events_url":"https://api.github.com/users/laravel/received_events","type":"Organization","site_admin":false},"repo":{"id":7548986,"name":"framework","full_name":"laravel/framework","owner":{"login":"laravel","id":958072,"avatar_url":"https://avatars.githubusercontent.com/u/958072?v=3","gravatar_id":"","url":"https://api.github.com/users/laravel","html_url":"https://github.com/laravel","followers_url":"https://api.github.com/users/laravel/followers","following_url":"https://api.github.com/users/laravel/following{/other_user}","gists_url":"https://api.github.com/users/laravel/gists{/gist_id}","starred_url":"https://api.github.com/users/laravel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/laravel/subscriptions","organizations_url":"https://api.github.com/users/laravel/orgs","repos_url":"https://api.github.com/users/laravel/repos","events_url":"https://api.github.com/users/laravel/events{/privacy}","received_events_url":"https://api.github.com/users/laravel/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/laravel/framework","description":"","fork":false,"url":"https://api.github.com/repos/laravel/framework","forks_url":"https://api.github.com/repos/laravel/framework/forks","keys_url":"https://api.github.com/repos/laravel/framework/keys{/key_id}","collaborators_url":"https://api.github.com/repos/laravel/framework/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/laravel/framework/teams","hooks_url":"https://api.github.com/repos/laravel/framework/hooks","issue_events_url":"https://api.github.com/repos/laravel/framework/issues/events{/number}","events_url":"https://api.github.com/repos/laravel/framework/events","assignees_url":"https://api.github.com/repos/laravel/framework/assignees{/user}","branches_url":"https://api.github.com/repos/laravel/framework/branches{/branch}","tags_url":"https://api.github.com/repos/laravel/framework/tags","blobs_url":"https://api.github.com/repos/laravel/framework/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/laravel/framework/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/laravel/framework/git/refs{/sha}","trees_url":"https://api.github.com/repos/laravel/framework/git/trees{/sha}","statuses_url":"https://api.github.com/repos/laravel/framework/statuses/{sha}","languages_url":"https://api.github.com/repos/laravel/framework/languages","stargazers_url":"https://api.github.com/repos/laravel/framework/stargazers","contributors_url":"https://api.github.com/repos/laravel/framework/contributors","subscribers_url":"https://api.github.com/repos/laravel/framework/subscribers","subscription_url":"https://api.github.com/repos/laravel/framework/subscription","commits_url":"https://api.github.com/repos/laravel/framework/commits{/sha}","git_commits_url":"https://api.github.com/repos/laravel/framework/git/commits{/sha}","comments_url":"https://api.github.com/repos/laravel/framework/comments{/number}","issue_comment_url":"https://api.github.com/repos/laravel/framework/issues/comments{/number}","contents_url":"https://api.github.com/repos/laravel/framework/contents/{+path}","compare_url":"https://api.github.com/repos/laravel/framework/compare/{base}...{head}","merges_url":"https://api.github.com/repos/laravel/framework/merges","archive_url":"https://api.github.com/repos/laravel/framework/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/laravel/framework/downloads","issues_url":"https://api.github.com/repos/laravel/framework/issues{/number}","pulls_url":"https://api.github.com/repos/laravel/framework/pulls{/number}","milestones_url":"https://api.github.com/repos/laravel/framework/milestones{/number}","notifications_url":"https://api.github.com/repos/laravel/framework/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/laravel/framework/labels{/name}","releases_url":"https://api.github.com/repos/laravel/framework/releases{/id}","created_at":"2013-01-10T21:27:28Z","updated_at":"2015-12-02T03:03:08Z","pushed_at":"2015-12-02T02:48:03Z","git_url":"git://github.com/laravel/framework.git","ssh_url":"git@github.com:laravel/framework.git","clone_url":"https://github.com/laravel/framework.git","svn_url":"https://github.com/laravel/framework","homepage":"http://laravel.com","size":32381,"stargazers_count":4428,"watchers_count":4428,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":2532,"mirror_url":null,"open_issues_count":57,"forks":2532,"open_issues":57,"watchers":4428,"default_branch":"5.1"}},"_links":{"self":{"href":"https://api.github.com/repos/laravel/framework/pulls/11069"},"html":{"href":"https://github.com/laravel/framework/pull/11069"},"issue":{"href":"https://api.github.com/repos/laravel/framework/issues/11069"},"comments":{"href":"https://api.github.com/repos/laravel/framework/issues/11069/comments"},"review_comments":{"href":"https://api.github.com/repos/laravel/framework/pulls/11069/comments"},"review_comment":{"href":"https://api.github.com/repos/laravel/framework/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/laravel/framework/pulls/11069/commits"},"statuses":{"href":"https://api.github.com/repos/laravel/framework/statuses/b99a35a39182c0be1b2a0ddce06daac04008ee4c"}}}}