Home > Software engineering >  Laravel Exists rule on mutliple columns with OR combiner
Laravel Exists rule on mutliple columns with OR combiner

Time:12-24

I want to have a rule that do something like following:

... from users where email = 'IDENTIFIER' or mobile = 'IDENTIFIER'
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class AuthIdentifyRequest extends FormRequest
{
    
    public function rules()
    {
        return [
            'identifier' => [
                'exists:users,email[OR]mobile'
            ]
        ];
    }
}

Is there any solution for this situation in Laravel?

I'm using Laravel 8.x

CodePudding user response:

I'm not sure this is the answer you are hoping for, but it should achieve the desired result.

public function rules()
{
    return [
        'identifier' => [
            // use the @ symbol to differentiate email and mobile inputs
            Str::contains($this->identifier,'@')
                ? 'exists:users,email'
                : 'exists:users,mobile'
        ],
    ];
}
  • Related