hipsterjazzbo / Landlord

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

Can't set $tenantColumns in model #99

Open m3steele opened 6 years ago

m3steele commented 6 years ago

I am unable to get $tenantColumns to set/change the column used for tenant lookup. I want to use this to change the column used for tenant in one of my tables.

public $tenantColumns = ['id'];

I have logged what tenantColumn is set to by using

$tenants = Landlord::getTenants();

I have set my default $tenantColumns in config and I set the scoped tenant in middleware.

Here is my model (shortened for clarity) where i am trying to change the $tenantColumn.

<?php
namespace App\Models\Admin;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use HipsterJazzbo\Landlord\BelongsToTenants;

class company extends Model
{
    use SoftDeletes;
    use BelongsToTenants;
    public $tenantColumns = ['id'];

...

One thing possibly worth noting is I was unable to successfully addTenant without clearing booted models before addTennant was called.

My middle wear looks as follows: Notice the addition of \Illuminate\Database\Eloquent\Model::clearBootedModels(); This was used as per instructions here: https://github.com/HipsterJazzbo/Landlord/issues/64

I wonder is this same issue is affecting my ability to set $tenantColumns.

use Closure;
use Sentinel;
use HipsterJazzbo\Landlord\Facades\Landlord;
use Illuminate\Support\Facades\Auth;

class Tenancy
{

    public function handle($request, Closure $next)
    {

        if (!Sentinel::check()) {
            return redirect('login')->with('info', 'You must be logged in!');
        }
        else {

            \Illuminate\Database\Eloquent\Model::clearBootedModels();
            Landlord::addTenant('company_id', $request->user()->company_id);

            return $next($request);
        }
    }

}

Thanks for any assistance.