-
-
Save bastiaandewaele/81d5bb37a612152e93a8eb8cc9d0ec59 to your computer and use it in GitHub Desktop.
UserRequest.php (rules)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Requests; | |
use Illuminate\Foundation\Http\FormRequest; | |
use Illuminate\Validation\Rule; | |
class UserRequest extends FormRequest | |
{ | |
public function rules(): array | |
{ | |
$rules = [ | |
'first_name' => ['string', 'min:3'], | |
'last_name' => ['string', 'min:3'] | |
]; | |
if ($this->method() === 'POST') { | |
$rules['first_name'][] = 'required'; | |
$rules['first_name'][] = 'unique:users,email'; | |
} | |
if ($this->method() === 'PUT') { | |
$rules['first_name'][] = Rule::unique('users', 'email') | |
->ignore($this->route('user')); | |
} | |
return $rules; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment