0

How can I validate variable if variable name is different then name of column in DB.

 'gameDetails.title' => [
                'required',
                'string',
                'between:3,100',
                Rule::unique('games')->ignore($this->input('gameId')),
            ],

In above example my input variable name is "gameDetails.title" but in DB my column name is just title. So in this case I have this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'gameDetails.title' in 'where clause'

So how to tell "laravel" my column name is just "title"?

Thank you.

1
  • Is gameDetails the name of your database table? Commented Oct 16, 2018 at 19:52

1 Answer 1

0

The unique validator takes a second param that is the name of the column in the database:

Rule::unique('games','title')->ignore($this->input('gameId'))

https://laravel.com/docs/5.7/validation#rule-unique

/**
 * Get a unique constraint builder instance.
 *
 * @param  string  $table
 * @param  string  $column
 * @return \Illuminate\Validation\Rules\Unique
 */
public static function unique($table, $column = 'NULL')
{
    return new Rules\Unique($table, $column);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for information. You did a misspelling, should be "Rule::unique('games', 'title')" I guess. Because my version works.

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.