Comments (6)
I'd really be interested in this, or knowing if it can be achieved already!
from landlord.
Since it's a pivot table the data would always be accessed through an already scoped model, so there would be no need to put tenant_id
on the pivot table directly, or am I missing something?
from landlord.
The features model would not be scoped, the pivot table is what defines the scope.
I would have three tables in this example:
features
: A list of all available features for my SaaS that tenants can activate/deactivate.tenant_features
: A pivot connecting the features with the tenants, effectively creating a list of which tenants have activated which features. Holds FKstenant_id
andfeature_id
.tenants
: The tenants table.
Getting all features for a tenant would look something like this:
$features = Features::whereHas('tenant', function ($query) use ($tenantId) {
$query->where('id', $tenantId);
})->get();
Ideally I would not want to be querying tenancy manually like this, it should work automatically.
One option which could work, would be to att a model for, and access features though, the pivot table?
from landlord.
This might work(I didn't test it). In your Feature class, add
public static function bootBelongsToTenantThroughPivot() {
static::addGlobalScope('tenant', function (Builder $builder) {
$builder->join('tenant_features', 'tenant_features.feature_id', '=', 'features.id')->select('features.*')->where('tenant_id', \Auth::user()->tenant_id);
});
}
It basically use the same idea(check src/BelongsToTenants.php src/TenantManager.php) as Landlord to insert tenant_id filter for every query. Then you can call
Features::get()
to get features for logged in user/tenant.
from landlord.
That isn't what this package is for. This package is for preventing data that belongs to Tenant A form leaking into queries run for Tenant B.
What you're trying to do is just a basic many-to-many anyway. You don't need this package, just do this:
class Tenant extends Model
{
public function features() {
return $this->belongsToMany(Feature::class);
}
}
and then:
$tenant = Tenant::find(1);
$tenant->features();
from landlord.
Not sure why I am having this problem too. My pivot table does have the trait on it. But it wasn't applying the tenant scope like the rest of my tables.
I should add that I'm doing something unconventional, I do not use the primary key (id) to make my joins. And as such my "keys" are not unique in the table, they are only unique when combined with the the tenant_id so you could say I use composite primary keys.
I was able to fix my problem by doing this
public function posts()
{
return $this->belongsToMany('Post', 'post_user', 'user_id', 'post_id', 'another_id', 'another_id')
->where('post_user.tenant_id', \Landlord::getTenants()->first());
}
from landlord.
Related Issues (20)
- how do we get current talnet?
- Still active? HOT 9
- Preserve tenant ID when executing queued job HOT 1
- Scope is not applied when model is instantiated via dependency injection within controller HOT 1
- Laravel 5.8 Support HOT 1
- Tenant Breaks when used with ModelObserver
- Example Middleware for Laravel 5.5 HOT 2
- BelongsToTenant not working on user model HOT 2
- WIth multiple tenants Landlord uses first tenant id for the other tenant scopes HOT 3
- Class landlord not found - Laravel 5.5 HOT 1
- Tenants getting mixed up HOT 1
- Multitenant not working with User model HOT 4
- Make Macroable
- Creating new Tenant scoped Models HOT 3
- Support Laravel 5.6 HOT 4
- Call allTenants() on a relationship HOT 1
- not working
- I am trying to install in Laravel 5.6 HOT 5
- Landlord does not work in Laravel 5.6 HOT 1
- Can't set $tenantColumns in model
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from landlord.