tenancy
tenancy copied to clipboard
PHP 8.1: Deprecation notice Accessing static trait property ($tenantIdColumn)
Bug description
I'm getting this deprecation notice (PHP 8.1):
PHP Deprecated: Accessing static trait property Stancl\Tenancy\Database\Concerns\BelongsToTenant::$tenantIdColumn is deprecated, it should only be accessed on a class using the trait in /var/www/html/vendor/stancl/tenancy/src/Database/TenantScope.php on line 20 PHP Deprecated: Accessing static trait property Stancl\Tenancy\Database\Concerns\BelongsToTenant::$tenantIdColumn is deprecated, it should only be accessed on a class using the trait in /var/www/html/vendor/stancl/tenancy/src/Database/Concerns/BelongsToTenant.php on line 19
Steps to reproduce
php artisan tinker
tenancy()->initialize(Tenant::first())
User::first()->tenant
Expected behavior
no messages
Laravel version
8.83.5
stancl/tenancy version
3.5.6
Thanks, we'll change this to use something like a config key.
fixed ?
fixed ?
@stancl please reopen this issue
Oki so for everyone else wondering, I sorted it out temporarily like this:
In config/tenancy.php
I added 'tenant_column' => 'tenant_id',
.
I copied the following files from vendor
to locations under app/
:
- \Stancl\Tenancy\Database\Concerns\BelongsToTenant::class
- \Stancl\Tenancy\Database\TenantScope::class
Inside of the boot method in app/Providers/AppServiceProvider.php
:
$this->app->bind(BelongsToTenant::class, \Stancl\Tenancy\Database\Concerns\BelongsToTenant::class);
$this->app->bind(TenantScope::class, \Stancl\Tenancy\Database\TenantScope::class);
In those files that I now have locally, I override any instance of BelongsToTenant::$tenantIdColumn
with config('tenancy.tenant_column')
Then in my models I just use the local trait use App\Helpers\Traits\BelongsToTenant;
.
I also ran php artisan config:clear
and composer dumpautoload -o
.
I stopped seeing PHP Deprecated
messages in tinker. Hope it helps.
I don't think the app->bind()
will have any effect, PHP can't be used like that
You're probably right that that bit might be nnecessary. In the past when fighting with Nova overrides on unrelated stuff, adding those bind calls helped. Think I dumped it there out of habit.
Here it just seems like you're binding your class to the package class (so it's in the opposite direction than what you'd want anyway), and I don't think it gets used because traits use PHP, not Laravel, so the service container bindings won't be able to change anything.
@stancl I submitted a PR that resolves this for me locally. I'm not sure that it's the direction you want to go with it but that PR resolves this issue.
Fixed in 4.x