To update the `RegisterRequest` contract in Laravel Fortify to include new fields, you can follow these steps:
1. Create a new file called `RegisterRequest.php` in your `app/Contracts/Fortify` directory.
php
<?php
namespace App\Contracts\Fortify;
use Illuminate\Validation\Rules;
interface RegisterRequest
{
/**
* Determine the validation rules for the registration request.
*
* @return array
*/
public function rules(): array;
/**
* Get the registration input.
*
* @return array
*/
public function input(): array;
/**
* Get the custom field value.
*
* @return string|null
*/
public function customField(): ?string;
}
2. Implement the `RegisterRequest` interface in your custom `RegisterRequest` class located in `app/Actions/Fortify/RegisterRequest.php`.
php
<?php
namespace App\Actions\Fortify;
use App\Contracts\Fortify\RegisterRequest as RegisterRequestContract;
use Illuminate\Validation\Rules;
class RegisterRequest implements RegisterRequestContract
{
/**
* Determine the validation rules for the registration request.
*
* @return array
*/
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'confirmed', Rules\Password::defaults()],
'custom_field' => ['required', 'string', 'max:100'], // Add your custom field
];
}
/**
* Get the registration input.
*
* @return array
*/
public function input(): array
{
return request()->all();
}
/**
* Get the custom field value.
*
* @return string|null
*/
public function customField(): ?string
{
return request()->input('custom_field');
}
}
3. Update the `CreateNewUser` class to use the new `RegisterRequest` contract.
php
<?php
namespace App\Actions\Fortify;
use App\Contracts\Fortify\RegisterRequest;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Laravel\Fortify\Contracts\CreatesNewUsers;
class CreateNewUser implements CreatesNewUsers
{
/**
* Validate and create a newly registered user.
*
* @param \App\Contracts\Fortify\RegisterRequest $request
* @return \App\Models\User
*/
public function create(RegisterRequest $request)
{
$user = User::create([
'name' => $request->input()['name'],
'email' => $request->input()['email'],
'password' => Hash::make($request->input()['password']),
'custom_field' => $request->customField(), // Use the custom field
]);
return $user;
}
}
4. Register the custom `RegisterRequest` class in your `FortifyServiceProvider`.
php
<?php
namespace App\Providers;
use App\Actions\Fortify\CreateNewUser;
use App\Actions\Fortify\RegisterRequest;
use App\Contracts\Fortify\RegisterRequest as RegisterRequestContract;
use Illuminate\Support\ServiceProvider;
use Laravel\Fortify\Fortify;
class FortifyServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Fortify::createUsersUsing(CreateNewUser::class);
Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
Fortify::registerView(function () {
return view('auth.register');
});
$this->app->bind(RegisterRequestContract::class, RegisterRequest::class);
}
}
By following these steps, you have created a custom `RegisterRequest` contract and implementation that includes your new custom field. The `CreateNewUser` class now uses the new `RegisterRequest` contract to access the custom field and create the user.
Remember to update your registration view to include the new form field, and ensure that the form submission matches the expected request structure.