hipsterjazzbo / Landlord

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

Non-static method HipsterJazzbo\Landlord\Landlord::addTenant #23

Closed pipegris closed 8 years ago

pipegris commented 8 years ago

Hi, I'm having this error: 'Non-static method HipsterJazzbo\Landlord\Landlord::addTenant() should not be called statically, assuming $this from incompatible context'...

When I try to put that line in a middleware

Landlord::addTenant('com_id', 2);

tnorthcutt commented 8 years ago

Did you add the alias to config/app.php as the readme instructs?

'aliases' => [
        ...
        'Landlord'   => HipsterJazzbo\Landlord\Facades\LandlordFacade::class,
    ],
pipegris commented 8 years ago

Thank you for your answer, Yes I did extactly as the readme says.

And my middleware: namespace App\Http\Middleware; use Closure; use HipsterJazzbo\Landlord\Landlord; class MultiTenant { public function handle($request, Closure $next, $guard = null) { Landlord::addTenant('com_id', 2); return $next($request); } }

My composer: require: "laravel/framework": "5.2.*", "hipsterjazzbo/landlord": "^1.0",

pipegris commented 8 years ago

I solved it injecting an instance of the class into the constructor of my Middleware. The code was like:

namespace App\Http\Middleware;
use Closure;
use HipsterJazzbo\Landlord\Landlord;
class MultiTenant {
    public function __construct(Landlord $landlord) {
        $this->landlord = $landlord;
    }

    public function handle($request, Closure $next, $guard = null) {
        $this->landlord->addTenant('com_id', 2);
        return $next($request);
    }
}

I was assuming literally the instructions in the readme, but after watching some laracasts I tried this and it worked, is this the intended kind of implementation? or is it just my lack of expertize. Thanks for your help.

hipsterjazzbo commented 8 years ago

Hi, your issue there when trying to use the Facade was that you were importing the actual HipsterJazzbo\Landlord\Landlord class.

When using a Facade, you should just use it without importing anything (\Landlord), and let the framework handle it.

Facades can be a bit confusing; I don't use them very often personally, but have a look here if you want a better explanation: https://laravel.com/docs/5.2/facades

pipegris commented 8 years ago

You are absolutely right, the problem raised because PHPStorm didn't recognize the Facades, and aparently pulled the import for the class from the indexed filesystem. Thank you so much.