StoreUserRequest
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
class StoreUserRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'name' => 'required|string',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
'role' => 'sometimes',
];
}
public function prepareForValidation(): void
{
$this->merge([
'email' => Str::of($this->email)->lower()->_toString(),
'name' => Str::of($this->name)->ucwords()->_toString(),
]);
}
protected function passedValidation()
{
$this->mergeIfMissing(['role' => 'user']);
}
}
UserController
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreUserRequest;
use App\Models\User;
class UserController extends Controller
{
public function register(StoreUserRequest $request)
{
$attributes = $request->validated();
$attributes['password'] = bcrypt($request->password);
User::create($attributes);
return response()->noContent();
}
}