1

Using laravel validation, I would like to ensure that a field is unique, but in an array context. (I have seen this and this but they don't address the array context.)

Let's suppose I have this html:

<input name="sites[1][id]"><input name="sites[1][site_mrn]">
<input name="sites[2][id]"><input name="sites[2][site_mrn]">
<input name="sites[3][id]"><input name="sites[3][site_mrn]">

In my validation rule, I want to ensure that each site's id is valid, and that the site_mrn is not blank, so I have:

public function rules()
{
    return [
        'sites.*.site_mrn' => 'required|min:1',
        'sites.*.id' => 'exists:sites,id'
    ];
 }

So that part works. My problem is that I want to ensure that each pair of site site_id and site_mrn are unique in the mpi_sites table, but I don't know how to access each id/site_mrn pair in the input. I want to do something like this:

'sites.*' => Rule::unique('mpi_sites')->where(function ($q) {
        $q->where('site_id', $xxxxx)->where('site_mrn', $yyyyy);
    })
2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.