Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

a-a's avatar
Level 1

Problem with validating array. unique triggers column not found laravel 5.6

I have this array and I want to validate it in form request. But I'm having trouble validating it.

array:9 [ "user_type" => 5

"student" => array:2 [ "student_code" => "12-0033" "student_number" => "10001-122" ]

"first_name" => "John" "middle_name" => "Smith" "last_name" => "Doe" "email" => "[email protected]" "password" => "a" "password_confirmation" => "a" "token" => "" ]

this is my validation code:

public function rules()
{
    $user_type = $this->input('user_type');
    $rules = [
        'first_name' => 'required',
        'middle_name' => 'required',
        'last_name' => 'required',
        'email' => 'required|email|unique:users',
        'password' => 'required|confirmed',
    ];

    if ($user_type == 5) {
        // Validate student
        $student_rules = [
            'student.student_code' => 'required|unique:students',
            'student.student_number' => 'required|unique:students',
        ];
        
        $rules = array_merge($rules, $student_rules);
}

    return $rules;
}

Everytime this is triggered I always get:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'student.student_code' in 'where clause' (SQL: select count(*) as aggregate from students where student.student_code = a)

It takes the 'student' from the 'student.student_code' as the table name.

What am I doing wrong? Newbie here.

I need some help. TIA

0 likes
2 replies
Cronix's avatar
Cronix
Best Answer
Level 67

unique:table,column,except,idColumn

The field under validation must be unique in a given database table. If the column option is not specified, the field name will be used.

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

Looks like you'd need

$student_rules = [
    'student.student_code' => 'required|unique:students,code',
    'student.student_number' => 'required|unique:students,number',
];

assuming the students table has a code and number column that you're checking against to be unique. Adjust as needed.

1 like
a-a's avatar
Level 1

Thanks bro. Adding ->unique(); to a column was the key.

Please or to participate in this conversation.