Search code examples
phplaravelrelationships

Undefined offset: 1 error for laravel validation


I have some validation on my form in laravel to ensure the gift the user is claiming is one assigned to their campaign with the below code

'gift_id' => 'required|int|exists:gifts,campaign_id,' . \Auth::user()->campaign->id,

But on form submission I am getting the following error

ErrorException in ValidatesAttributes.php line 721: Undefined offset: 1

Could someone please help me out?

Thanks


Solution

  • You need a more custom exists rule. According to the docs exists as a string can only handle the existence of a specific input value in the specific column:

    'gift_id' => [
        'required',
        'int',
        Rule::exists('gifts')->where(function ($query) {
            $query->where('campaign_id', \Auth::user()->campaign->id);
        }),
    ],
    

    or something to this effect.