hipsterjazzbo / Landlord

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

allTenants Call to a member function newQueryWithoutTenants() on null #77

Open rikvdlooi opened 6 years ago

rikvdlooi commented 6 years ago

When calling ::allTenants() on a model that has not been booted I get the following error.

PHP error:  Call to a member function newQueryWithoutTenants() on null in vendor/hipsterjazzbo/landlord/src/BelongsToTenants.php

I found that this is due to that the Model has not yet booted (it was never instantiated). This means the bootBelongsToTenants() will never get called, so in turn $landlord will never be set to a TenantManager.

I'm using the newest version of LandLord with Laravel 5.5.

rikvdlooi commented 6 years ago

I see a PR was created to fix this. Pull Request #75

rikvdlooi commented 6 years ago

Workaround for people stumbling upon this: instantiate the model you want to call allTenants() on. Example:

use App\Books;

public function getAllBooks()
{
    // Workaround to call boot method, needed for Landlord to work correctly.
    new Books;

    return Books::allTenants()->get();
}