Search code examples
laravel-5laravel-validation

Laravel 5.2 exists validation cannot find column


I have wierd trouble during validating request in controller. I use exists validation rule. User is supposed to submit integer. This integer should exist as id in a single row of user_courses table. This row also should have column user_id equal to 158 - docs.

My validation rule:

'course_id' => 'integer|exists: user_courses, id, user_id, 158'

What I got instead:

PDOException in Connection.php line 333: SQLSTATE[42S22]: Column not found: 1054 Unknown column ' id' in 'where clause'

My Table:

id, user_id, created_at, updated_at

Solution

  • Problem was in the spaces:

    'course_id' => 'integer|exists: user_courses, id, user_id, 158' // incorrect
    
    'course_id' => 'integer|exists:user_courses,id,user_id,158' // correct
    

    Original code would confuse laravel to search for column names like this:

    ' user_courses'