Add audit owner through parent model

Models that belong to an organization via a parent model (for example
project members via their project) implement AuditableThroughParent and
return the parent relation. The owner organization of their audits is
taken from the loaded parent if it matches the foreign key, otherwise it
is queried. The declaration is also used to backfill the owner of
existing audits.
This commit is contained in:
Constantin Graf
2026-10-07 16:05:45 +02:00
committed by Constantin Graf
parent 6fe721fc26
commit 34273a4863
4 changed files with 96 additions and 6 deletions

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Models;
use App\Models\Concerns\AuditableThroughParent;
use App\Models\Concerns\CustomAuditable;
use App\Models\Concerns\HasUuids;
use Database\Factories\ProjectMemberFactory;
@@ -29,7 +30,7 @@ use OwenIt\Auditing\Contracts\Auditable as AuditableContract;
* @method static Builder<ProjectMember> whereBelongsToOrganization(Organization $organization)
* @method static ProjectMemberFactory factory()
*/
class ProjectMember extends Model implements AuditableContract
class ProjectMember extends Model implements AuditableContract, AuditableThroughParent
{
use CustomAuditable;
@@ -83,11 +84,11 @@ class ProjectMember extends Model implements AuditableContract
});
}
public function getAuditOwnerOrganizationId(): ?string
/**
* @return BelongsTo<Project, $this>
*/
public function getAuditParentRelation(): BelongsTo
{
/** @var string|null $organizationId */
$organizationId = Project::query()->whereKey($this->project_id)->value('organization_id');
return $organizationId;
return $this->project();
}
}