Skip to content

Instantly share code, notes, and snippets.

@bastiaandewaele
Last active March 12, 2021 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bastiaandewaele/81d5bb37a612152e93a8eb8cc9d0ec59 to your computer and use it in GitHub Desktop.
Save bastiaandewaele/81d5bb37a612152e93a8eb8cc9d0ec59 to your computer and use it in GitHub Desktop.
UserRequest.php (rules)
<?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