#`->unique()` rule inside relationship instead of whole table

2 messages · Page 1 of 1 (latest)

verbal plover
#

Hey guys,
I have this url /{clientSlug}/{productSlug}
On ClientResource, I have a ->unique(ignoreRecord: true) on the slug field and this works fine.
Now I want to be able to create multiple product with the same productSlug unless they have the same parent (client).
I found ->unique(modifyRuleUsing: function (Unique $rule, $record) but I'm not sure how to implement this.

Basically, I want to be able to do /client-1/product-1 and /client-2/product-1.
But I don't want to be able to create multiple product-1 on client-1

verbal plover
#

I think I got it:

Forms\Components\Select::make('client_id')
    ->relationship('client', 'name')
    ->required()
    ->default(request()->query('ownerRecord')),
Forms\Components\TextInput::make('slug')
    ->unique(
        ignoreRecord: true,
        modifyRuleUsing: function (Unique $rule, $get) {
        return $rule->where('client_id', $get('client_id'));
    })
    ->default('site-web'),