37 lines
1,001 B
PHP
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);
|
|
});
|
|
}
|
|
}
|