26 lines
773 B
PHP
26 lines
773 B
PHP
<?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
|
|
*
|
|
* @return \Closure
|
|
*/
|
|
public static function whereTenantQuery(
|
|
\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());
|
|
};
|
|
};
|
|
}
|
|
}
|