0
votes

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? - adam

1 Answers

0
votes

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);
}