ldc-com-app-bo/app/Traits/Models/IsRelatedToTenant.php

27 lines
773 B
PHP
Raw Normal View History

2025-06-17 12:29:15 +02:00
<?php
namespace App\Traits\Models;
use App\Models\Tenant;
trait IsRelatedToTenant
{
/**
* Add a 'where' clause to the given query to scope to the current or first tenant.
*
* @param \App\Models\Tenant|\Stancl\Tenancy\Contracts\Tenant|null $tenantModel
*
2025-06-17 12:29:15 +02:00
* @return \Closure
*/
public static function whereTenantQuery(
2026-10-20 16:03:44 +02:00
\App\Models\Tenant|\Stancl\Tenancy\Contracts\Tenant|null $tenantModel = null
): \Closure {
return function ($query) use ($tenantModel) {
$tenant = $tenantModel ?: (!\is_null(\tenant()) ? \tenant() : Tenant::first());
if (\tenant() || Tenant::first()) {
$query->where('tenant_id', $tenant->getKey() ?? $tenant->getKey());
};
2025-06-17 12:29:15 +02:00
};
}
}