Last active
May 23, 2022 12:27
Revisions
-
dillinghamio revised this gist
May 26, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains 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 @@ 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](https://github.com/laravel/framework/pull/13548) --- -
dillinghamio revised this gist
May 26, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains 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,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 -
dillinghamio revised this gist
May 23, 2016 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains 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 @@ -39,9 +39,9 @@ trait UsedByTeams static::teamGuard(); if( ! isset($model->team_id)) { $model->team_id = auth()->user()->currentTeam->id; } }); } -
dillinghamio revised this gist
May 23, 2016 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains 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 @@ -38,10 +38,10 @@ trait UsedByTeams { static::teamGuard(); if( ! isset($model->team_id)) { $model->team_id = auth()->user()->currentTeam->id; } }); } -
dillinghamio revised this gist
May 23, 2016 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
This file contains 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 @@ -37,8 +37,11 @@ trait UsedByTeams static::saving(function (Model $model) { static::teamGuard(); if(static::hasGlobalScope('team')) { $model->team_id = auth()->user()->currentTeam->id; } }); } -
dillinghamio revised this gist
May 3, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains 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,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 -
dillinghamio revised this gist
May 3, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains 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,4 +1,4 @@ ## UsedByTeams Model Trait For [Laravel Spark](https://spark.laravel.com/) > Automatically limit your models to the current team -
dillinghamio revised this gist
May 3, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains 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,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`. -
dillinghamio revised this gist
May 3, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains 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 @@ -57,7 +57,7 @@ trait UsedByTeams } } ``` ### Usage -
dillinghamio revised this gist
May 3, 2016 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains 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 @@ -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(); ``` -
dillinghamio revised this gist
May 3, 2016 . 1 changed file with 6 additions and 9 deletions.There are no files selected for viewing
This file contains 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 @@ -80,18 +80,15 @@ class Task extends Model ### Methods ```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(); ``` -
dillinghamio revised this gist
May 3, 2016 . 1 changed file with 2 additions and 9 deletions.There are no files selected for viewing
This file contains 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 @@ -88,16 +88,9 @@ Task::create(); // gets all tasks Task::allTeams()->get(); // get all tasks with team eagerloaded Task::allTeams()->with('team')->get() ``` ##### What if I want to remove the scope? -
dillinghamio revised this gist
May 3, 2016 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains 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 @@ -62,8 +62,6 @@ I also added the ability to access the team from the model, like `$model->team() ### Usage ```PHP <?php -
dillinghamio revised this gist
May 3, 2016 . 1 changed file with 4 additions and 5 deletions.There are no files selected for viewing
This file contains 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,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 ```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 -
dillinghamio revised this gist
May 3, 2016 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains 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 @@ -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`. ### 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 ### Usage The Model @@ -82,7 +82,7 @@ class Task extends Model } ``` ### Methods ```PHP // gets all the teams tasks -
dillinghamio revised this gist
May 3, 2016 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains 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 @@ -88,7 +88,8 @@ Usage // gets all the teams tasks Task::all(); // automaticly adds team_id Task::create(); // gets all tasks Task::allTeams()->get(); -
dillinghamio revised this gist
May 3, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains 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 @@ -88,6 +88,8 @@ Usage // gets all the teams tasks Task::all(); Task::create(); // automaticly adds team_id // gets all tasks Task::allTeams()->get(); ``` -
dillinghamio revised this gist
May 3, 2016 . 1 changed file with 11 additions and 4 deletions.There are no files selected for viewing
This file contains 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 @@ -28,16 +28,15 @@ trait UsedByTeams static::addGlobalScope('team', function(Builder $builder) { 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 -
dillinghamio revised this gist
May 3, 2016 . 1 changed file with 15 additions and 7 deletions.There are no files selected for viewing
This file contains 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 @@ -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(); 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) { $model->team_id = auth()->user()->currentTeam->id; }); } public function scopeAllTeams($query) -
dillinghamio revised this gist
May 2, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains 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 @@ -84,6 +84,6 @@ $tasks = Task::allTeams()->get(); foreach($tasks as $task) { $task->team()->name; } ``` -
dillinghamio revised this gist
May 2, 2016 . 1 changed file with 6 additions and 2 deletions.There are no files selected for viewing
This file contains 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 @@ -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) { $taskTeam = $task->team()->name; } ``` -
dillinghamio revised this gist
May 2, 2016 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains 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 @@ -76,8 +76,6 @@ Task::all(); // gets all tasks Task::allTeams()->get(); $tasks = Task::allTeams()->get(); foreach($tasks as $task) -
dillinghamio revised this gist
May 2, 2016 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains 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 @@ -70,9 +70,11 @@ class Task extends Model Usage ```PHP // gets all the teams tasks Task::all(); // gets all tasks Task::allTeams()->get(); //------- -
dillinghamio revised this gist
May 2, 2016 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains 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 @@ -80,5 +80,6 @@ $tasks = Task::allTeams()->get(); foreach($tasks as $task) { // get team associated with the task $taskTeam = $task->team()->name; } -
dillinghamio revised this gist
May 2, 2016 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains 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 @@ -77,3 +77,8 @@ Task::allTeams()->get(); // gets all tasks //------- $tasks = Task::allTeams()->get(); foreach($tasks as $task) { $taskTeam = $task->team()->name; } -
dillinghamio revised this gist
May 2, 2016 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains 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 @@ -73,3 +73,7 @@ Usage Task::all(); // gets all the teams tasks Task::allTeams()->get(); // gets all tasks //------- $tasks = Task::allTeams()->get(); -
dillinghamio revised this gist
May 2, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains 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 @@ -58,8 +58,8 @@ The Model namespace App; use App\Traits\UsedByTeams; use Illuminate\Database\Eloquent\Model; class Task extends Model { -
dillinghamio revised this gist
May 2, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains 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 @@ -69,7 +69,7 @@ class Task extends Model Usage ```PHP Task::all(); // gets all the teams tasks Task::allTeams()->get(); // gets all tasks -
dillinghamio revised this gist
May 2, 2016 . 1 changed file with 8 additions and 1 deletion.There are no files selected for viewing
This file contains 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 @@ -65,4 +65,11 @@ class Task extends Model { use UsedByTeams; } ``` Usage ``` Task::all(); // gets all the teams tasks Task::allTeams()->get(); // gets all tasks -
dillinghamio revised this gist
May 2, 2016 . 1 changed file with 21 additions and 1 deletion.There are no files selected for viewing
This file contains 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 @@ -45,4 +45,24 @@ trait UsedByTeams } } ``` 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; } ```
NewerOlder