Home > Mobile >  unique validate using laravel
unique validate using laravel

Time:12-07

this is my controller

 $rules = [ 
  'Group_Id'=>'required',
  'Group_Code'=>'required', 
  'Group_Name'=>'required| string', 
  'Currency'=>'required', 
  'Country'=>'required', 
  'State'=>'required', 
  'City'=>'required',
]

I want group id Group code should be unique while create if group id /group code already present in table means I want to print error as group id/group id already exit .while updating don't want to change group id filed that's remains same

CodePudding user response:

For solving this issue you should add unique identifier in your validation method mentioned as below

 $data = $request->validate([
            'Group_Id' => 'required',
            'Group_Code' => 'required|unique:table_name_here',
            'Group_Name' => 'required| string',
            'Currency' => 'required',
            'Country' => 'required',
            'State' => 'required',
            'City' => 'required',
        ]);

CodePudding user response:

$rules = [ 
  'Group_Id'=>'required|unique:group,id',
  'Group_Code'=>'required|unique:group,code', 
  'Group_Name'=>'required| string', 
  'Currency'=>'required', 
  'Country'=>'required', 
  'State'=>'required', 
  'City'=>'required',
]
  • Related