Skip to content

Instantly share code, notes, and snippets.

@sgelbart
Last active September 22, 2016 09:55
Show Gist options
  • Select an option

  • Save sgelbart/781f6d483601d2a5efd8 to your computer and use it in GitHub Desktop.

Select an option

Save sgelbart/781f6d483601d2a5efd8 to your computer and use it in GitHub Desktop.

Revisions

  1. sgelbart revised this gist Dec 8, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion LaravelEasyGlobalScope.php
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@

    public function scopeGlobal($query)
    {
    //todo: there's currently a bug where where you can't use nested wheres!!
    //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
    }

  2. sgelbart revised this gist Dec 8, 2015. 1 changed file with 18 additions and 6 deletions.
    24 changes: 18 additions & 6 deletions LaravelEasyGlobalScope.php
    Original 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 EasyGlobalScope {
    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());
    }
    }
    ?>
  3. sgelbart renamed this gist Aug 24, 2015. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions LaravelEasyGlobalScope → LaravelEasyGlobalScope.php
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,8 @@

    public function scopeGlobal($query)
    {
    return $query->where('is_published',1); //replace this with whatever criteria you ahve
    //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;
    return $query;
    }


    @@ -47,7 +48,7 @@ public function scopeGlobal($query)
    */
    public function newQuery ()
    {
    return parent::newQuery()->global();
    return parent::newQuery()->global();
    }

    /**
    @@ -61,8 +62,8 @@ public function newQuery ()
    */
    public static function noEasyGlobal()
    {
    $instance = new Static;
    return $instance->newQueryNoEasyGlobal();
    $instance = new Static;
    return $instance->newQueryNoEasyGlobal();
    }

    /**
    @@ -73,7 +74,7 @@ public static function noEasyGlobal()
    */
    public function newQueryNoEasyGlobal()
    {
    return parent::newQuery();
    return parent::newQuery();
    }
    }
    ?>
  4. sgelbart revised this gist Aug 24, 2015. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion LaravelEasyGlobalScope
    Original file line number Diff line number Diff line change
    @@ -3,10 +3,14 @@
    //USAGE

    //in your model
    public function scopeGlobal($query){

    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

  5. sgelbart revised this gist Aug 24, 2015. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions LaravelEasyGlobalScope
    Original 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
  6. sgelbart revised this gist Jun 29, 2015. 1 changed file with 34 additions and 32 deletions.
    66 changes: 34 additions & 32 deletions LaravelEasyGlobalScope
    Original 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(){

    trait EasyGlobalScope {
    /**
    * @param $query
    * @return mixed
    */
    * @param $query
    * @return mixed
    */
    public function scopeGlobal($query)
    {
    return $query;
    return $query;
    }


    /**
    * Override all newQuery instances
    *
    * @return mixed
    */
    * Override all newQuery instances
    *
    * @return mixed
    */
    public function newQuery ()
    {
    return parent::newQuery()->global();
    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
    */
    * 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();
    $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
    */
    * 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();
    return parent::newQuery();
    }
    }
    ?>
  7. sgelbart revised this gist Jun 29, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion LaravelEasyGlobalScope
    Original 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(){
    trait EasyGlobalScope(){

    /**
    * @param $query
  8. sgelbart revised this gist Jun 29, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion LaravelEasyGlobalScope
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@

    trait easyGlobalScope(){

    /**
    /**
    * @param $query
    * @return mixed
    */
  9. sgelbart revised this gist Jun 29, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions LaravelEasyGlobalScope
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,7 @@
    **/

    trait easyGlobalScope(){

    /**
    * @param $query
    * @return mixed
  10. sgelbart revised this gist Jun 29, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions LaravelEasyGlobalScope
    Original 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
  11. sgelbart created this gist Jun 29, 2015.
    57 changes: 57 additions & 0 deletions LaravelEasyGlobalScope
    Original 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();
    }
    }