I'm having trouble getting the Validator to work with the rule that I want to set up. I could have made multiple tables, but it's going to be a lot of work to undo everything, so I'd prefer to make it work if possible.
$validator = Validator::make($input, [
'segment_name' => [
'required',
Rule::unique('groups')->where(function ($query) {
$query->where('lang', $list_name)->where('group_type', 'auto_segment');
}),
],
]);
I thought query was the way to do it, but I now realise that this adds another constraint instead. What I want is the validation to fail when the 2 query conditions are met, but otherwise then it should pass. Is there any easy way to do this?
After spending more time working on this, I've made some more attempts, but it hasn't yet worked. Using 2 rules, the validator fails when I would want it to pass, similar to using || in an if statement instead of &&. Using a pipe between the two rules failed and generated an offset error, but this might not have been correct.
'segment_name' => 'unique:groups,segment_name,NULL,id,lang,' . $list_name,
'segment_name' => 'unique:groups,segment_name,NULL,id,group_type,auto_segment'
'segment_name' => 'unique:groups,segment_name,NULL,id,lang,' . $list_name . '|unique:groups,segment_name,NULL,id,group_type,auto_segment'
After finally getting the original query to work without variables, ended up using a global variable to pass to the query, as a local wouldn't work.