Skip to content

Instantly share code, notes, and snippets.

@dillinghamio
Last active May 23, 2022 12:27
Show Gist options
  • Save dillinghamio/a110c1b3bad8055f4327838bc4b11cd3 to your computer and use it in GitHub Desktop.
Save dillinghamio/a110c1b3bad8055f4327838bc4b11cd3 to your computer and use it in GitHub Desktop.

UsedByTeams Model Trait For Laravel Spark

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.

Usage

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

namespace App\Traits;

use Illuminate\Database\Eloquent\Builder;

trait UsedByTeams
{
	protected static function boot()
    {
        parent::boot();
        
        if(! auth()->guest())
        {
	        static::addGlobalScope('team', function(Builder $builder)
	        {
	            $builder->where('team_id', auth()->user()->currentTeam->id);
	        });        	
        }
    }

    public function scopeAllTeams($query)
    {
    	return $query->withoutGlobalScope('team');
    }

    public function team()
    {
    	return $this->belongsTo('App\Team');
    }
}

I also added the ability to access the team from the model, like $model->team()->name etc

Example

The Model

<?php

namespace App;

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

class Task extends Model
{
	use UsedByTeams;
}

Usage

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

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

//-------

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

foreach($tasks as $task)
{
	// get team associated with the task
	$taskTeam = $task->team()->name;
}
@tnorthcutt
Copy link

First off: thanks for sharing this, it's been super helpful!

I've run into an issue with it where trying to do things via API or with artisan commands returns this:

[Exception]
  No Auth User/Team

Commenting out most of UsedByTeams.php resolves the issue, but obviously that's a kludgy workaround that is best avoided. Do you have any tips on how to more selectively apply or disable the trait so this isn't an issue?

@parweb
Copy link

parweb commented Nov 18, 2016

@tnorthcutt i think it's because when you do ajax it doesn't know that your are loggin.
In other words it can't read the session variable.

You need to pass the api_key or the internal TOKEN like vuejs do in spark kiosks

@xplodedthemes
Copy link

What about queued jobs ? Since jobs do not care about Authentication or sessions, if a job requires to perform some action on scoped models, it will fail. Not sure if it's a good idea to pass the user & team id to the job and authenticate before each job handling. Especially if jobs are being processed under a daemon, it's not reliable at all to use sessions within a job.
This has been driving me crazy for weeks now, any help on what should be the proper way of using this with queued jobs would be very much appreciated :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment