【问题标题】:Laravel 使用 OR 组合器在多列上存在规则
【发布时间】:2022-01-23 15:57:36
【问题描述】:

我想要一个规则来做以下事情:

... 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'
            ]
        ];
    }
}

在 Laravel 中有没有针对这种情况的解决方案?

我正在使用 Laravel 8.x

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    我不确定这是否是您希望的答案,但它应该可以达到预期的结果。

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

    【讨论】:

      猜你喜欢
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2020-10-31
      • 1970-01-01
      相关资源
      最近更新 更多