Skip to content

Instantly share code, notes, and snippets.

@dillinghamio
Last active May 23, 2022 12:27

Revisions

  1. dillinghamio revised this gist May 26, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ So you're using spark, and you have teams enabled. You start creating models and

    This assumes that the model has a `team_id`, while it adds a scope of `where team_id = currentTeam->id`.

    > Note: Implicit Route Model Binding in 5.2, auth session doesn't exist at the point of this trait causing issue. fixed in 5.3
    > Note: Implicit Route Model Binding in 5.2, auth session doesn't exist at the point of this trait causing issue. [fixed in 5.3](https://github.com/laravel/framework/pull/13548)
    ---

  2. dillinghamio revised this gist May 26, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,8 @@ So you're using spark, and you have teams enabled. You start creating models and

    This assumes that the model has a `team_id`, while it adds a scope of `where team_id = currentTeam->id`.

    > Note: Implicit Route Model Binding in 5.2, auth session doesn't exist at the point of this trait causing issue. fixed in 5.3
    ---

    ### Setup
  3. dillinghamio revised this gist May 23, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -39,9 +39,9 @@ trait UsedByTeams
    static::teamGuard();

    if( ! isset($model->team_id))
    {
    $model->team_id = auth()->user()->currentTeam->id;
    }
    {
    $model->team_id = auth()->user()->currentTeam->id;
    }
    });
    }

  4. dillinghamio revised this gist May 23, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -38,10 +38,10 @@ trait UsedByTeams
    {
    static::teamGuard();

    if(static::hasGlobalScope('team'))
    {
    $model->team_id = auth()->user()->currentTeam->id;
    }
    if( ! isset($model->team_id))
    {
    $model->team_id = auth()->user()->currentTeam->id;
    }
    });
    }

  5. dillinghamio revised this gist May 23, 2016. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -37,8 +37,11 @@ trait UsedByTeams
    static::saving(function (Model $model)
    {
    static::teamGuard();

    $model->team_id = auth()->user()->currentTeam->id;

    if(static::hasGlobalScope('team'))
    {
    $model->team_id = auth()->user()->currentTeam->id;
    }
    });
    }

  6. dillinghamio revised this gist May 3, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,8 @@ So you're using spark, and you have teams enabled. You start creating models and

    This assumes that the model has a `team_id`, while it adds a scope of `where team_id = currentTeam->id`.

    ---

    ### Setup

    Make a file, namespaced etc
  7. dillinghamio revised this gist May 3, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ### `UsedByTeams` Model Trait For [Laravel Spark](https://spark.laravel.com/)
    ## UsedByTeams Model Trait For [Laravel Spark](https://spark.laravel.com/)

    > Automatically limit your models to the current team
  8. dillinghamio revised this gist May 3, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    ### `UsedByTeams` Model Trait For [Laravel Spark](https://spark.laravel.com/)

    > Automatically limit your models to the current team
    So you're using spark, and you have teams enabled. You start creating models and want to have them be team specific. Instead of writing, `Model::where('team_id', auth()->user()->currentTeam->id)->get();` use this trait to add that behind the scenes so that every time you call on your model, it's assumed that you mean for the current team.

    This assumes that the model has a `team_id`, while it adds a scope of `where team_id = currentTeam->id`.
  9. dillinghamio revised this gist May 3, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -57,7 +57,7 @@ trait UsedByTeams
    }
    }
    ```
    I also added the ability to access the team from the model, like `$model->team()->name` etc



    ### Usage
  10. dillinghamio revised this gist May 3, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -81,14 +81,18 @@ class Task extends Model

    ```PHP
    // gets current teams tasks

    Task::all();

    // automaticly adds current team_id

    Task::create();

    // gets all tasks / all teams globally

    Task::allTeams()->get();

    // get all tasks with task's team eagerloaded

    Task::allTeams()->with('team')->get();
    ```
  11. dillinghamio revised this gist May 3, 2016. 1 changed file with 6 additions and 9 deletions.
    15 changes: 6 additions & 9 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -80,18 +80,15 @@ class Task extends Model
    ### Methods

    ```PHP
    // gets all the teams tasks
    // gets current teams tasks
    Task::all();

    // automaticly adds team_id
    // automaticly adds current team_id
    Task::create();

    // gets all tasks
    // gets all tasks / all teams globally
    Task::allTeams()->get();

    // get all tasks with team eagerloaded
    Task::allTeams()->with('team')->get()
    ```

    ##### What if I want to remove the scope?
    Simply add `allTeams` to your query, `Model::allTeams()->get()` and the model won't be limited by the current team
    // get all tasks with task's team eagerloaded
    Task::allTeams()->with('team')->get();
    ```
  12. dillinghamio revised this gist May 3, 2016. 1 changed file with 2 additions and 9 deletions.
    11 changes: 2 additions & 9 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -88,16 +88,9 @@ Task::create();

    // gets all tasks
    Task::allTeams()->get();
    ```
    Get team associated with the task

    ```PHP
    $tasks = Task::allTeams()->get();

    foreach($tasks as $task)
    {
    $task->team()->name;
    }
    // get all tasks with team eagerloaded
    Task::allTeams()->with('team')->get()
    ```

    ##### What if I want to remove the scope?
  13. dillinghamio revised this gist May 3, 2016. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -62,8 +62,6 @@ I also added the ability to access the team from the model, like `$model->team()

    ### Usage

    The Model

    ```PHP

    <?php
  14. dillinghamio revised this gist May 3, 2016. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -9,9 +9,6 @@ This assumes that the model has a `team_id`, while it adds a scope of `where tea
    Make a file, namespaced etc
    Add `use UsedByTeams` to your model

    ##### What if I want to remove the scope?
    Simply add `allTeams` to your query, `Model::allTeams()->get()` and the model won't be limited by the current team

    ```PHP
    <?php

    @@ -94,7 +91,6 @@ Task::create();
    // gets all tasks
    Task::allTeams()->get();
    ```

    Get team associated with the task

    ```PHP
    @@ -104,4 +100,7 @@ foreach($tasks as $task)
    {
    $task->team()->name;
    }
    ```
    ```

    ##### What if I want to remove the scope?
    Simply add `allTeams` to your query, `Model::allTeams()->get()` and the model won't be limited by the current team
  15. dillinghamio revised this gist May 3, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ So you're using spark, and you have teams enabled. You start creating models and

    This assumes that the model has a `team_id`, while it adds a scope of `where team_id = currentTeam->id`.

    ### Usage
    ### Setup

    Make a file, namespaced etc
    Add `use UsedByTeams` to your model
    @@ -63,7 +63,7 @@ trait UsedByTeams
    I also added the ability to access the team from the model, like `$model->team()->name` etc


    ### Example
    ### Usage

    The Model

    @@ -82,7 +82,7 @@ class Task extends Model
    }
    ```

    Usage
    ### Methods

    ```PHP
    // gets all the teams tasks
  16. dillinghamio revised this gist May 3, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -88,7 +88,8 @@ Usage
    // gets all the teams tasks
    Task::all();

    Task::create(); // automaticly adds team_id
    // automaticly adds team_id
    Task::create();

    // gets all tasks
    Task::allTeams()->get();
  17. dillinghamio revised this gist May 3, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -88,6 +88,8 @@ Usage
    // gets all the teams tasks
    Task::all();

    Task::create(); // automaticly adds team_id

    // gets all tasks
    Task::allTeams()->get();
    ```
  18. dillinghamio revised this gist May 3, 2016. 1 changed file with 11 additions and 4 deletions.
    15 changes: 11 additions & 4 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -28,16 +28,15 @@ trait UsedByTeams

    static::addGlobalScope('team', function(Builder $builder)
    {
    if(auth()->guest() || ! auth()->user()->currentTeam)
    {
    throw new \Exception('No Auth User/Team');
    }
    static::teamGuard();

    $builder->where('team_id', auth()->user()->currentTeam->id);
    });

    static::saving(function (Model $model)
    {
    static::teamGuard();

    $model->team_id = auth()->user()->currentTeam->id;
    });
    }
    @@ -51,6 +50,14 @@ trait UsedByTeams
    {
    return $this->belongsTo('App\Team');
    }

    protected static function teamGuard()
    {
    if(auth()->guest() || ! auth()->user()->currentTeam)
    {
    throw new \Exception('No Auth User/Team');
    }
    }
    }
    ```
    I also added the ability to access the team from the model, like `$model->team()->name` etc
  19. dillinghamio revised this gist May 3, 2016. 1 changed file with 15 additions and 7 deletions.
    22 changes: 15 additions & 7 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -18,20 +18,28 @@ Simply add `allTeams` to your query, `Model::allTeams()->get()` and the model wo
    namespace App\Traits;

    use Illuminate\Database\Eloquent\Builder;
    use Illuminate\Database\Eloquent\Model;

    trait UsedByTeams
    {
    protected static function boot()
    {
    parent::boot();

    if(! auth()->guest())

    static::addGlobalScope('team', function(Builder $builder)
    {
    if(auth()->guest() || ! auth()->user()->currentTeam)
    {
    throw new \Exception('No Auth User/Team');
    }

    $builder->where('team_id', auth()->user()->currentTeam->id);
    });

    static::saving(function (Model $model)
    {
    static::addGlobalScope('team', function(Builder $builder)
    {
    $builder->where('team_id', auth()->user()->currentTeam->id);
    });
    }
    $model->team_id = auth()->user()->currentTeam->id;
    });
    }

    public function scopeAllTeams($query)
  20. dillinghamio revised this gist May 2, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -84,6 +84,6 @@ $tasks = Task::allTeams()->get();

    foreach($tasks as $task)
    {
    $taskTeam = $task->team()->name;
    $task->team()->name;
    }
    ```
  21. dillinghamio revised this gist May 2, 2016. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -75,11 +75,15 @@ Task::all();

    // gets all tasks
    Task::allTeams()->get();
    ```

    Get team associated with the task

    ```PHP
    $tasks = Task::allTeams()->get();

    foreach($tasks as $task)
    {
    // get team associated with the task
    $taskTeam = $task->team()->name;
    }
    }
    ```
  22. dillinghamio revised this gist May 2, 2016. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -76,8 +76,6 @@ Task::all();
    // gets all tasks
    Task::allTeams()->get();

    //-------

    $tasks = Task::allTeams()->get();

    foreach($tasks as $task)
  23. dillinghamio revised this gist May 2, 2016. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -70,9 +70,11 @@ class Task extends Model
    Usage

    ```PHP
    Task::all(); // gets all the teams tasks
    // gets all the teams tasks
    Task::all();

    Task::allTeams()->get(); // gets all tasks
    // gets all tasks
    Task::allTeams()->get();

    //-------

  24. dillinghamio revised this gist May 2, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -80,5 +80,6 @@ $tasks = Task::allTeams()->get();

    foreach($tasks as $task)
    {
    // get team associated with the task
    $taskTeam = $task->team()->name;
    }
  25. dillinghamio revised this gist May 2, 2016. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -77,3 +77,8 @@ Task::allTeams()->get(); // gets all tasks
    //-------

    $tasks = Task::allTeams()->get();

    foreach($tasks as $task)
    {
    $taskTeam = $task->team()->name;
    }
  26. dillinghamio revised this gist May 2, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -73,3 +73,7 @@ Usage
    Task::all(); // gets all the teams tasks

    Task::allTeams()->get(); // gets all tasks

    //-------

    $tasks = Task::allTeams()->get();
  27. dillinghamio revised this gist May 2, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -58,8 +58,8 @@ The Model

    namespace App;

    use Illuminate\Database\Eloquent\Model;
    use App\Traits\UsedByTeams;
    use Illuminate\Database\Eloquent\Model;

    class Task extends Model
    {
  28. dillinghamio revised this gist May 2, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -69,7 +69,7 @@ class Task extends Model

    Usage

    ```
    ```PHP
    Task::all(); // gets all the teams tasks

    Task::allTeams()->get(); // gets all tasks
  29. dillinghamio revised this gist May 2, 2016. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -65,4 +65,11 @@ class Task extends Model
    {
    use UsedByTeams;
    }
    ```
    ```

    Usage

    ```
    Task::all(); // gets all the teams tasks
    Task::allTeams()->get(); // gets all tasks
  30. dillinghamio revised this gist May 2, 2016. 1 changed file with 21 additions and 1 deletion.
    22 changes: 21 additions & 1 deletion UsedByTeams.md
    Original file line number Diff line number Diff line change
    @@ -45,4 +45,24 @@ trait UsedByTeams
    }
    }
    ```
    I also added the ability to access the team from the model, like `$model->team()->name` etc
    I also added the ability to access the team from the model, like `$model->team()->name` etc


    ### Example

    The Model

    ```PHP

    <?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;
    use App\Traits\UsedByTeams;

    class Task extends Model
    {
    use UsedByTeams;
    }
    ```