Skip to content

Instantly share code, notes, and snippets.

@tnorthcutt
Forked from dillinghamio/UsedByTeams.md
Last active April 5, 2019 14:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tnorthcutt/13d62d68737ab2a1222adb84122788f9 to your computer and use it in GitHub Desktop.
Save tnorthcutt/13d62d68737ab2a1222adb84122788f9 to your computer and use it in GitHub Desktop.

UsedByTeams Model Trait For Laravel Spark

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.

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

Make a file, namespaced etc: see UsedByTeams.php file below. Add use UsedByTeams to your model

Usage

<?php

namespace App;

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

class Task extends Model
{
	use UsedByTeams;
}

Methods

// 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();
<?php
namespace App\Traits;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
/**
* Trait UsedByTeams
*
* Forked from https://gist.github.com/dillinghamio/a110c1b3bad8055f4327838bc4b11cd3
*
* @package App\Traits
*/
trait UsedByTeams
{
protected static function boot()
{
parent::boot();
static::addGlobalScope('team', function (Builder $builder) {
static::teamGuard();
if (static::normalContext()) {
$builder->where('team_id', auth()->user()->currentTeam->id);
}
});
static::saving(function (Model $model) {
static::teamGuard();
if (! isset($model->team_id)) {
$model->team_id = auth()->user()->currentTeam->id;
}
});
}
public function scopeAllTeams($query)
{
return $query->withoutGlobalScope('team');
}
public function team()
{
return $this->belongsTo(\App\Models\Team::class);
}
protected static function teamGuard()
{
if (static::shouldBeGuarded()) {
throw new \Exception('No Auth User/Team');
}
}
private static function shouldBeGuarded()
{
if (static::normalContext() && static::userHasNoTeam()) {
return true;
}
return false;
}
private static function normalContext()
{
if (! \App::runningInConsole() && ! \App::runningUnitTests() && ! request()->is('nova*')) {
return true;
}
return false;
}
private static function userHasNoTeam()
{
if (auth()->guest() || !auth()->user()->currentTeam) {
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment