hipsterjazzbo / Landlord

A simple, single database multi-tenancy solution for Laravel 5.2+
MIT License
614 stars 138 forks source link

Scope not being applied #25

Closed g-psantos closed 8 years ago

g-psantos commented 8 years ago

I installed and configured Landlord, and created a route to test if it was working. The use case is very simple: I have a User model and an Account model, where a User can have multiple Accounts. The Accounts table has a "user_id", and the Eloquent relationship is defined in both models as well.

// app/Http/routes.php
Route::get('test/{id}', function($id) {
    Landlord::addTenant('user_id', $id);

    return view('test')->with('accounts', App\Account::all());
});
// app/Account.php

namespace App;

use Illuminate\Database\Eloquent\Model;
use HipsterJazzbo\Landlord\BelongsToTenant;

class Account extends Model
{
    use BelongsToTenant;

    ...
}

When I access that route, I'm still getting the accounts for all the users. Debugbar shows the SQL query:

select * from `accounts`

Any ideas on what's missing?

g-psantos commented 8 years ago

I figured out from looking at other issues in the project that each model has to be configured with a protected $tenantColumns array or, alternatively, a default must be set by publishing the component's configuration file. I hadn't published that file and was unaware of the need to set that variable in each model, which is why it wasn't working.

I'd suggest adding this step to the documentation.