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

noblemfd's avatar

Laravel Unique Rules for multiple columns

Kindly pardon me, I just started using Laravel Unique Rules

I have this table in Laravel-5.8:

exams: id, exam_category_id, exam_name, exam_year, student_id

Model class is Exam and the route in route list is exam

exam_category_id, exam_name, exam_year, student_id are uniquely tied together. Four of them are together

public function rules()
{

}

This is not working:

Rule::unique('exams', 'exam_category_id', 'exam_name', 'exam_year', 'student_id')

        'exam_category_id' => [
           'required',              
           Rule::unique('exams', 'exam_category_id', 'exam_name', 'exam_year', 'student_id')
        ], 

Once you entered it, it won't allow any other student with the same ;'exam_category_id', 'exam_name', 'exam_year'

I have only used unique rule for one column. How can I make the four (4) columns to be unique together both for create and update?

Thank you.

0 likes
10 replies
Sti3bas's avatar
Sti3bas
Best Answer
Level 53

You may also specify additional query constraints by customizing the query using the where method. Something like this:

'exam_category_id' => Rule::unique('exams')->where(function ($query) use ($request) {
   return $query->where('exam_name', $request->exam_name)
      ->where('exam_year', $request->exam_year)
      ->where('student_id', $request->student_id);
})

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

6 likes
jeevamugunthan's avatar

try this

before use this in your conroller

use Illuminate\Support\Facades\Validator;

public function rules(Request $request)
    {
$validator = Validator::make($request->all(), [
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            ]);
}

here users is a table name

noblemfd's avatar

I got this error:

Undefined variable: request

noblemfd's avatar

Do I also need to replace ($query) use ($request) with $this

noblemfd's avatar

How do I apply the same thing for update

vlauciani's avatar

Hi all

Using the code:

Rule::unique('exams')->where(function ($query) use ($request) { 
. . .
}

is there a way to customize the output message?

Thank you,

laugre's avatar

Hi Same question @vlauciani how to handle a custom error message on a multiple field validation ? I have a form request validation rule :

public function rules()
    {
        return [
                'name' => 'required',
                'email' => 'required|email',
                'username' =>  [
                             'required', 
                             Rule::unique('users')
                                    ->where('company_id', $this->company_id)
                            ]
            ];
    }

and a message handler :

public function messages()
    {
        return [
            'username.unique' => 'couple username and company_id has to be unique.',
        ];
    }

Validation rule well pass or not, but my custom message doesn't appear. Any idea ? Thx

3 likes

Please or to participate in this conversation.