unique is not working

18 Replies
ba_mbi_07
ba_mbi_07OP2y ago
even though my taxonomy is something else other than category ...it still checking slug for unique
John
John2y ago
I don't think the ->unique() validator can be used like this. The first param it expects is $table. Perhaps you need a custom validation rule? https://filamentphp.com/docs/2.x/forms/validation#custom-rules
ba_mbi_07
ba_mbi_07OP2y ago
yes i have used rules but how to use ignoreRecord
ba_mbi_07
ba_mbi_07OP2y ago
ba_mbi_07
ba_mbi_07OP2y ago
why unique this way is not working
John
John2y ago
The callback on unique is used to extend the unique query that's already inside the unique validation rule.
ba_mbi_07
ba_mbi_07OP2y ago
but how to use whereHas condition its giving error
John
John2y ago
Please check #✅┊rules on how to paste PHP code in here instead of screenshots. It makes it easier to read and possible to copy paste. What if you use a simple ->unique with callback, and build from there? At which point does it break and what's the error message?
ba_mbi_07
ba_mbi_07OP2y ago
TextInput::make('slug')
->label('Kheltag Slug')
->unique(callback: function (Unique $rule) {
return $rule->whereHas('taxonomy', function ($query) {
$query->where('taxonomy', 'kheltags');
});
}),
TextInput::make('slug')
->label('Kheltag Slug')
->unique(callback: function (Unique $rule) {
return $rule->whereHas('taxonomy', function ($query) {
$query->where('taxonomy', 'kheltags');
});
}),
how to use whereHas here because it showing me error here Undefined method 'whereHas'.
Dan Harrin
Dan Harrin2y ago
check the laravel docs, i dont think that method exists
ba_mbi_07
ba_mbi_07OP2y ago
Then can you tell me if i have to check two tables data for unique what to do ??
Dan Harrin
Dan Harrin2y ago
its in the laravel docs, wrap it in where() $rule->where(fn ($query) => $query->whereHas(
ba_mbi_07
ba_mbi_07OP2y ago
TextInput::make('slug')
->label('Kheltag Slug')
->unique(callback: function (Unique $rule) {
// dd($rule);
return $rule->where(fn ($query) => $query->whereHas('taxonomy', function ($query) {
$query->where('taxonomy', 'kheltags');
}));
}),
TextInput::make('slug')
->label('Kheltag Slug')
->unique(callback: function (Unique $rule) {
// dd($rule);
return $rule->where(fn ($query) => $query->whereHas('taxonomy', function ($query) {
$query->where('taxonomy', 'kheltags');
}));
}),
Still did not work
Dan Harrin
Dan Harrin2y ago
you need to debug it then i cant hold your hand for everything
John
John2y ago
Define "still did not work" ? Do you have a query log to see which queries are executed? Laravel Debugbar is an easy way to add logging/monitoring. What are you trying to accomplish anyway? Because now you're checking if there is already a model in the database with the same slug as what you are trying to save, that also has a related taxonomy record with taxonomy "kheltags". Which means that you can still add records with the same slug as a record in the database, except if that relation exists on the db record .
ba_mbi_07
ba_mbi_07OP2y ago
what I am trying to check is that it will check same slug in the table wp_terms but not entirely that table but it will other table wp_term_taxonomy which have foreign key column term_id where taxonomy column value is "kheltags" and those data will be checked giving me this error SQLSTATE[42S22]: Column not found: 1054 Unknown column 'has' in 'where clause'
TextInput::make('slug')
->label('Kheltag Slug')
->rules([
function ($record) {
return function ($attribute, $value, $fail) use ($record) {
$test = $_SERVER['REQUEST_URI'];
if (!str_contains($test, 'edit') || $record?->slug !== $value) {
if (WpTerm::where('slug', $value)
->whereHas('taxonomy', function ($query) {
$query->where('taxonomy', 'kheltags');
})->exists()
) {
$fail("The {$value} must be unique in the 'Kheltag' taxonomy.");
}
};
};
},

]),
TextInput::make('slug')
->label('Kheltag Slug')
->rules([
function ($record) {
return function ($attribute, $value, $fail) use ($record) {
$test = $_SERVER['REQUEST_URI'];
if (!str_contains($test, 'edit') || $record?->slug !== $value) {
if (WpTerm::where('slug', $value)
->whereHas('taxonomy', function ($query) {
$query->where('taxonomy', 'kheltags');
})->exists()
) {
$fail("The {$value} must be unique in the 'Kheltag' taxonomy.");
}
};
};
},

]),
I made it work this way but still wanted do it in unique()
John
John2y ago
Strange. Eloquent "creates" magic functions, e.g. for column name you can use ->where('name', ...) or ->whereName('...). But still whereHas should be available too. Xdebug is very handy in debugging cases like this. Check it out if you want. Otherwise, try to debug however you can. Anyway, you've got at least a working solution.
ba_mbi_07
ba_mbi_07OP2y ago
okay thank you
Want results from more Discord servers?
Add your server