Level 104
'name' => Rule::unique('categories')->where(function ($query) use ($request) {
return $query->where('user_id', $request->user()->id);
})
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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!
Please or to participate in this conversation.