GithubHelp home page GithubHelp logo

Many-to-many? about landlord HOT 6 CLOSED

hipsterjazzbo avatar hipsterjazzbo commented on August 22, 2024
Many-to-many?

from landlord.

Comments (6)

jpmurray avatar jpmurray commented on August 22, 2024 2

I'd really be interested in this, or knowing if it can be achieved already!

from landlord.

lasseeee avatar lasseeee commented on August 22, 2024

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.

torkiljohnsen avatar torkiljohnsen commented on August 22, 2024

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 FKs tenant_id and feature_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.

tsalufe avatar tsalufe commented on August 22, 2024

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.

hipsterjazzbo avatar hipsterjazzbo commented on August 22, 2024

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.

vesper8 avatar vesper8 commented on August 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.