feat: multi-tenant SaaS implementation - admin interface, tenant isolation, and UI updates

This commit is contained in:
jeremy bayse
2026-03-28 18:38:22 +01:00
parent 7d94be7a8c
commit f53d5770df
20 changed files with 757 additions and 34 deletions

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Traits;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
trait BelongsToTenant
{
protected static function bootBelongsToTenant()
{
static::addGlobalScope('tenant', function (Builder $builder) {
if (Auth::check()) {
$user = Auth::user();
if ($user->role === 'super_admin') {
// Super admins see everything
return;
}
if ($user->tenant_id) {
$builder->where('tenant_id', $user->tenant_id);
}
}
});
static::creating(function ($model) {
if (Auth::check() && Auth::user()->tenant_id && Auth::user()->role !== 'super_admin') {
$model->tenant_id = Auth::user()->tenant_id;
}
});
}
public function tenant()
{
return $this->belongsTo(\App\Models\Tenant::class);
}
}