2

I have subject tables and fields are standard_id, stream_id, medium_id, board_id, subject_name and many others.

I want to add Rule unique for subject_name which is unique for standard_id, stream_id, medium_id, board_id fields.

$validator = Validator::make( $inputs, 
            [
                'v_name' => [
                        'required',
                        Rule::unique( 'tbl_subject' )->ignore( $id, 'id' ),
                    ],
            ],
            [
                'v_name.required' => 'Name is required',
                'unique' => 'Name address already exits.',
            ]
        );

Example

standard_id, stream_id, medium_id, board_id subject_name

1             2            1            3          A
3             2            4            1          B
1             3            1            4          c

Validation like if subject_name "A" unique for 1, 2, 1, 3. B unique for 3, 2, 4, 1. But subject_name "A" not unique for 2,2,1,3 and so on..

9
  • 1
    Can you show what you have tried so far and how it doesn't match what you need. Commented Jul 18, 2020 at 7:09
  • So for example it would fail if you got a subject_name of A for the 1, 2, 1, 3 combination of inputs? Are those inputs in your request as well? Try implementing something like what is mentioned in Adding additional where clauses Commented Jul 18, 2020 at 7:13
  • You using Form Requests? Or validating in controller? Commented Jul 18, 2020 at 8:08
  • @KurtFriars Validation in controller. Commented Jul 18, 2020 at 9:26
  • @AmitSenjaliya Can you please share the code where you are validating Commented Jul 18, 2020 at 9:26

1 Answer 1

4

*** After discussion with OP ***

The question should have stated that they are trying to insert a single record, and want to verify the uniqueness against the database. One way to do this is via a custom rule, like so:

class UniqueSubject implements Rule
{
    private $keys;

    public function __construct(array $keys) {
        $this->keys = $keys;
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        return ! Subject::where($this->keys)->where('subject_name', $value)->exists();
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The Subject Name must be unique for the given standard, stream, medium and board.';
    }
}

Then you can use the validation in your rules like:

$keys = $request->only('i_standard_id', 'i_stream_id', 'i_board_id', 'i_medium_id');

$validator = Validator::make( $inputs, [
    ...
    'subject_name' => [
        'required',
        'string',
        new UniqueSubject($keys)
    ],
    ...
]);
Sign up to request clarification or add additional context in comments.

1 Comment

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.