Unique record within tenancy rather than across the board?

As the title suggests, I'm attempting to tighten up one of my tables with the 'unique' property and would like to scope the limitations entirely to the tenant rather than across the board. For example:
TextInput::make('slug')
->unique(Blogs::class, 'slug', ignoreRecord: true, modifyRuleUsing:
fn (Unique $rule, string $context, ?Model $record) =>
$rule->where('organizations_id', $record->organizations_id)
),
TextInput::make('slug')
->unique(Blogs::class, 'slug', ignoreRecord: true, modifyRuleUsing:
fn (Unique $rule, string $context, ?Model $record) =>
$rule->where('organizations_id', $record->organizations_id)
),
This should work for editing, but I can see a flaw for creation, when the record doesn't yet exist. What is the most efficient way to get this id other than by extracting it from the user? I ask as I have created the ability to create content for my clients and would prefer to have checks account for this as there may be issues in this case.
4 Replies
Sjoerd24
Sjoerd245mo ago
If you are using tenants, this is a better solution:
Forms\Components\TextInput::make('slug')
->required()
->unique(
ignoreRecord: true,
modifyRuleUsing: function (Unique $rule) {
return $rule->where('organizations_id', Filament::getTenant()->id);
}),
Forms\Components\TextInput::make('slug')
->required()
->unique(
ignoreRecord: true,
modifyRuleUsing: function (Unique $rule) {
return $rule->where('organizations_id', Filament::getTenant()->id);
}),
Steve_OH
Steve_OHOP5mo ago
The only issue is that I currently have the ability to help my tenants create records as part of development and would like the scope to be for the record rather than my own tenancy. I can’t create records on behalf of other tenants if the unique is based on my tenancy rather than theirs as there may be a match on theirs I’m not aware of
Sjoerd24
Sjoerd245mo ago
Maybe use for helping the impersonate plugin or something?
Steve_OH
Steve_OHOP5mo ago
I'm wondering if it's possible to take the data from maybe a select field? Like, if I select the organization I can then have the filter applied to that organization rather than having to find a convoluted solution involving impersonation or logging in as another user
Want results from more Discord servers?
Add your server