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

himala's avatar

How to add an exception to unique laravel rule validation

Hi guys, I have a validation that goes like this

My user can create many categories for his/her product and my categories table looks like this

id user_id name created_at updated_at

CategoryController.php

// Creates category

public function create() { $data = request()->validate([ 'name' => 'required|unique for that user_id ]); }

What I want to happen is it checks if there's an existing category_name under that user_id

Can you help me please? Thanks in advance!

0 likes
2 replies
tykus's avatar
'name' => Rule::unique('categories')->where(function ($query) use ($request) {
	return $query->where('user_id', $request->user()->id);
})
1 like

Please or to participate in this conversation.