Last active
September 22, 2016 09:55
-
-
Save sgelbart/781f6d483601d2a5efd8 to your computer and use it in GitHub Desktop.
Revisions
-
sgelbart revised this gist
Dec 8, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ public function scopeGlobal($query) { //todo: there may be a bug where you can't use nested wheres..try with ->where(function($query){return MyModel::noEasyGlobal();} return MyModel::noEasyGlobal()->where('is_published',1); //replace this with whatever criteria you ahve } -
sgelbart revised this gist
Dec 8, 2015 . 1 changed file with 18 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -29,8 +29,8 @@ public function scopeGlobal($query) * (noEasyGlobal must be called STATICALLY, so User::where(...)->noEasyGlobal() WONT WORK) **/ trait LaravelEasyGlobalScope { /** * @param $query * @return mixed @@ -39,18 +39,19 @@ public function scopeGlobal($query) { return $query; } /** * Override all newQuery instances * * @return mixed */ public function newQuery () { return parent::newQuery()->global(); } /** * This method overrides all the global scopes * MUST BE CALLED STATICALLY @@ -65,7 +66,7 @@ public static function noEasyGlobal() $instance = new Static; return $instance->newQueryNoEasyGlobal(); } /** * Return newQuery without global scope * Note: this WILL use any other scopes applied Laravel's traditional way @@ -76,5 +77,16 @@ public function newQueryNoEasyGlobal() { return parent::newQuery(); } /** * Get a new query builder that doesn't have any global scopes. * By default Model's method calls newQuery and we end up with recursion if we don't change to newQueryNoEasyGlobal * * @return \Illuminate\Database\Eloquent\Builder|static */ public function newQueryWithoutScopes() { return $this->removeGlobalScopes($this->newQueryNoEasyGlobal()); } } ?> -
sgelbart renamed this gist
Aug 24, 2015 . 1 changed file with 7 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,8 @@ public function scopeGlobal($query) { //todo: there's currently a bug where where you can't use nested wheres!! return MyModel::noEasyGlobal()->where('is_published',1); //replace this with whatever criteria you ahve } //in your controllers @@ -36,7 +37,7 @@ trait EasyGlobalScope { */ public function scopeGlobal($query) { return $query; } @@ -47,7 +48,7 @@ public function scopeGlobal($query) */ public function newQuery () { return parent::newQuery()->global(); } /** @@ -61,8 +62,8 @@ public function newQuery () */ public static function noEasyGlobal() { $instance = new Static; return $instance->newQueryNoEasyGlobal(); } /** @@ -73,7 +74,7 @@ public static function noEasyGlobal() */ public function newQueryNoEasyGlobal() { return parent::newQuery(); } } ?> -
sgelbart revised this gist
Aug 24, 2015 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,10 +3,14 @@ //USAGE //in your model public function scopeGlobal($query) { return $query->where('is_published',1); //replace this with whatever criteria you ahve } //in your controllers MyModel::all(); //has global MyModel::noEasyGlobal(); //no global -
sgelbart revised this gist
Aug 24, 2015 . 1 changed file with 14 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,17 @@ <?php //USAGE //in your model public function scopeGlobal($query){ return $query->where('is_published',1); //replace this with whatever criteria you ahve } //in your controllers MyModel::all(); //has global MyModel::noEasyGlobal(); //no global ?> <?php /** * EASY SCOPE GLOBAL -
sgelbart revised this gist
Jun 29, 2015 . 1 changed file with 34 additions and 32 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ <?php /** * EASY SCOPE GLOBAL * * by Sabrina Gelbart * Note: has not been thoroughly tested! * To use, include this trait in your models @@ -9,51 +10,52 @@ * (noEasyGlobal must be called STATICALLY, so User::where(...)->noEasyGlobal() WONT WORK) **/ trait EasyGlobalScope { /** * @param $query * @return mixed */ public function scopeGlobal($query) { return $query; } /** * Override all newQuery instances * * @return mixed */ public function newQuery () { return parent::newQuery()->global(); } /** * This method overrides all the global scopes * MUST BE CALLED STATICALLY * E.g., User::noEasyGlobal()->where(... will work. User::where(..)->noEasyGlobal() will NOT work! * * This needs to be a static method which just wraps the non-static newQueryNoEasyGlobal (might be better way to do this) * * @return mixed */ public static function noEasyGlobal() { $instance = new Static; return $instance->newQueryNoEasyGlobal(); } /** * Return newQuery without global scope * Note: this WILL use any other scopes applied Laravel's traditional way * * @return mixed */ public function newQueryNoEasyGlobal() { return parent::newQuery(); } } ?> -
sgelbart revised this gist
Jun 29, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ * (noEasyGlobal must be called STATICALLY, so User::where(...)->noEasyGlobal() WONT WORK) **/ trait EasyGlobalScope(){ /** * @param $query -
sgelbart revised this gist
Jun 29, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ trait easyGlobalScope(){ /** * @param $query * @return mixed */ -
sgelbart revised this gist
Jun 29, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,6 +10,7 @@ **/ trait easyGlobalScope(){ /** * @param $query * @return mixed -
sgelbart revised this gist
Jun 29, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,6 +8,7 @@ * to UNDO the scope use syntax User::noEasyGlobal()->where(.. * (noEasyGlobal must be called STATICALLY, so User::where(...)->noEasyGlobal() WONT WORK) **/ trait easyGlobalScope(){ /** * @param $query -
sgelbart created this gist
Jun 29, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,57 @@ /** * EASY SCOPE GLOBAL * * by Sabrina Gelbart * Note: has not been thoroughly tested! * To use, include this trait in your models * then override the scopeGlobal method in your Model * to UNDO the scope use syntax User::noEasyGlobal()->where(.. * (noEasyGlobal must be called STATICALLY, so User::where(...)->noEasyGlobal() WONT WORK) **/ trait easyGlobalScope(){ /** * @param $query * @return mixed */ public function scopeGlobal($query) { return $query; } /** * Override all newQuery instances * * @return mixed */ public function newQuery () { return parent::newQuery()->global(); } /** * This method overrides all the global scopes * MUST BE CALLED STATICALLY * E.g., User::noEasyGlobal()->where(... will work. User::where(..)->noEasyGlobal() will NOT work! * * This needs to be a static method which just wraps the non-static newQueryNoEasyGlobal (might be better way to do this) * * @return mixed */ public static function noEasyGlobal() { $instance = new Static; return $instance->newQueryNoEasyGlobal(); } /** * Return newQuery without global scope * Note: this WILL use any other scopes applied Laravel's traditional way * * @return mixed */ public function newQueryNoEasyGlobal() { return parent::newQuery(); } }