resolveOrganisationId(); if ($orgId === null) { return; } $fieldIds = FormField::query() ->withoutGlobalScope(OrganisationScope::class) ->whereIn( 'form_schema_id', FormSchema::query() ->withoutGlobalScope(OrganisationScope::class) ->where('organisation_id', $orgId) ->select('id'), ) ->select('id'); $libraryIds = FormFieldLibrary::query() ->withoutGlobalScope(OrganisationScope::class) ->where('organisation_id', $orgId) ->select('id'); $table = $model->getTable(); $builder->where(function (Builder $outer) use ($table, $fieldIds, $libraryIds): void { $outer->where(function (Builder $q) use ($table, $fieldIds): void { $q->where("$table.owner_type", 'form_field') ->whereIn("$table.owner_id", $fieldIds); })->orWhere(function (Builder $q) use ($table, $libraryIds): void { $q->where("$table.owner_type", 'form_field_library') ->whereIn("$table.owner_id", $libraryIds); }); }); } private function resolveOrganisationId(): ?string { if ($this->organisationId !== null) { return $this->organisationId; } $route = request()->route(); if ($route === null) { return null; } $org = $route->parameter('organisation'); if ($org instanceof \App\Models\Organisation) { return $org->id; } if (is_string($org) && $org !== '') { return $org; } $event = $route->parameter('event'); if ($event instanceof \App\Models\Event) { return $event->organisation_id; } return null; } }