Replies: 1 comment
-
You could make the framework/src/Illuminate/Validation/Rules/DatabaseRule.php Lines 48 to 53 in 6bed63f Technically, the framework/src/Illuminate/Validation/Rules/Unique.php Lines 53 to 59 in 6bed63f but as the docs state, Laravel doesn't support composite keys natively, so I'm not surprise, that it doesn't.
It's already possible to use use Illuminate\Validation\Rule;
class MyRequest extends FormRequest
{
public function rules(): array
return [
'user_id' => [ Rule::unique(User::class, 'id')->using(function($query) use($user) {
$query->where([ 'id' => $this->user_id, 'name' => $this->user_name ]);
}) ],
];
}
} You might customize the error message in that case, which is "The user id has already been taken." But the thing is: The Laravel validation rules are built to validate each field separately, so this would break a bit with that convention. |
Beta Was this translation helpful? Give feedback.
-
Hi everyone.
I ask you if is possible implementing an rule built-in as actual unique rule but applied on composite key (unique or primary)?
Beta Was this translation helpful? Give feedback.
All reactions