lernschiff/app/Traits/BelongsToTenant.php

22 lines
511 B
PHP

<?php
namespace App\Traits;
use App\Scopes\TenantScope;
trait BelongsToTenant
{
protected static function bootBelongsToTenant(): void
{
static::addGlobalScope(new TenantScope());
static::creating(function ($model) {
$user = auth()->user();
if ($user && !$user->hasRole('platform-admin')) {
// Force tenant_id — prevents cross-tenant mass-assign attacks
$model->tenant_id = $user->tenant_id;
}
});
}
}