tenancy icon indicating copy to clipboard operation
tenancy copied to clipboard

PHP 8.1: Deprecation notice Accessing static trait property ($tenantIdColumn)

Open oscar-ol opened this issue 2 years ago • 8 comments

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

oscar-ol avatar Apr 29 '22 11:04 oscar-ol

Thanks, we'll change this to use something like a config key.

stancl avatar May 01 '22 10:05 stancl

fixed ?

ahmedsayedabdelsalam avatar Jun 29 '22 11:06 ahmedsayedabdelsalam

fixed ?

hai-shift avatar Jul 07 '22 19:07 hai-shift

@stancl please reopen this issue

oscar-ol avatar Jul 25 '22 13:07 oscar-ol

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.

garethnic avatar Aug 17 '22 10:08 garethnic

I don't think the app->bind() will have any effect, PHP can't be used like that

stancl avatar Aug 17 '22 15:08 stancl

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.

garethnic avatar Aug 17 '22 16:08 garethnic

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 avatar Aug 17 '22 16:08 stancl

@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.

zach2825 avatar Sep 30 '22 20:09 zach2825

Fixed in 4.x

stancl avatar Oct 01 '22 16:10 stancl