Valid field serial_number from model Product as unique as an example;
Note if we are updating the record, and the value of record field is equals to value of request (received from form submit), it's necessary ignore because didn't have alterations in this field. Otherwise, it will validate as unique field.
class ProductRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
switch ($this->method()) {
case 'GET': {
return [];
}
case 'DELETE': {
return [];
}
case 'POST': {
return [
'fire_stamp' => 'required|unique:products,serial_number',
];
}
case 'PUT': {
return [
'fire_stamp' => $this->product->serial_number == $this->get('serial_number') ? Rule::unique('products')->ignore($this->get('serial_number'), 'serial_number') : 'required|unique:products,serial_number',
];
}
default:
break;
}
}
}