How to Use the Same Request Validation Rules for Storing and Updating in Laravel
In this lesson, we will see how to use the same request validation rules for storing and updating in Laravel, sometimes we want to use the same validation rules of the store method without creating another file for the update method so how can we do that?
Use the same validation rules for store and update methods
To do that use the code below:
public function rules()
{
return [
'name' => 'required',
'email' => $this->getMethod() == 'POST' ?
'required|string|unique:users' :
'required|string|unique:users,email,'.$this->id,
'password' => 'required|min:8',
];
}