Closed kjdion84 closed 7 years ago
Same problem here.
@genesluna & @kjdion84 - have you checked to make sure you're pulling in the BelongsToTenant trait in the models you want to scope (the Role model in the example above?)
I'm using 2.0.5 and I've just set up this simple test as I'm using Landlord in an app I'm working on:
/** @test */
public function setScoping()
{
$accountOne = factory(Account::class)->create();
$accountTwo = factory(Account::class)->create();
\Landlord::addTenant($accountTwo);
factory(UserProfile::class)->create(['account_id' => $accountOne->id,]);
factory(UserProfile::class)->create(['account_id' => $accountTwo->id,]);
$userProfiles = UserProfile::all();
$this->assertEquals(1, $userProfiles->count());
$this->assertEquals(2, $userProfiles->first()->id);
}
Which works just as expected (e.g. the test passes) if the trait is in place and fails (showing all UserProfiles) if the trait is removed which sounds like the behaviour you're experiencing
I'm using this package in over a dozen different large production applications and it absolutely does work.
Without more information I can't help you fix this issue, but almost always it winds up being because you need to make sure you're adding tenants before you need them.
I guess you are using the package Laravel Permission from Spatie. This package boots the Role model in its service provider. If you use a middleware to set the tenant it will not work on the Role model because by this time it is already booted.
Solution: call \Illuminate\Database\Eloquent\Model::clearBootedModels();
before adding the tenant.
See also #61.
Sebastian
I have a
Role
model. I've publish the config file, set the model to use the trait, and also made sure thattenant_id
exists in theroles
table. I've also calledLandlord::addTenant('tenant_id', $tenant->id);
in my group middleware.When I dump
getTenants()
on the page I get:Well when I run this simple code:
It is echoing the roles that belong to any tenant. This does not even work at all.