presseportale/app/Scopes/PortalScope.php
Kevin Adametz 5b8bdf4182
Some checks are pending
linter / quality (push) Waiting to run
tests / ci (push) Waiting to run
12-05-2026 Frontend dev
2026-05-12 18:32:33 +02:00

37 lines
1,001 B
PHP

<?php
namespace App\Scopes;
use App\Enums\Portal;
use App\Services\CurrentPortalContext;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
/**
* Filtert Eloquent-Queries automatisch auf das aktive Portal.
*
* Einträge mit portal = 'both' sind immer sichtbar.
* Kein Filter wenn: kein Portal-Kontext gesetzt (CLI, Tests, Import-Commands).
*
* Deaktivieren für einzelne Queries:
* Company::withoutGlobalScope(PortalScope::class)->get();
*/
class PortalScope implements Scope
{
public function apply(Builder $builder, Model $model): void
{
$portal = CurrentPortalContext::get();
if ($portal === null) {
return;
}
$table = $model->getTable();
$builder->where(function (Builder $q) use ($table, $portal): void {
$q->where("{$table}.portal", $portal->value)
->orWhere("{$table}.portal", Portal::Both->value);
});
}
}