To add custom fields to the registration form generated by Laravel Breeze, you'll need to make modifications to the views, controllers, and migrations. Here's a step-by-step guide:
If you need to add custom fields to the users table, modify the migration file located at database/migrations/create_users_table.php
. Add the desired columns within the up
method. For example:
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('custom_field'); // Add custom field here
$table->timestamp('email_verified_at')->nullable();
$table->rememberToken();
$table->timestamps();
});
Then, run the migration to apply the changes:
php artisan migrate
Modify the registration view to include input fields for your custom fields. You can find the registration view in resources/views/auth/register.blade.php
. Add input fields similar to:
<div class="mt-4">
<label for="custom_field" class="block font-medium text-sm text-gray-700">Custom Field</label>
<input id="custom_field" type="text" name="custom_field" value="{{ old('custom_field') }}" required autocomplete="custom_field" class="form-input rounded-md shadow-sm mt-1 block w-full"/>
</div>
If necessary, update the validation rules for the registration form. You can find the validation rules in the app/Http/Controllers/Auth/RegisterController.php
file, within the validator
method.
Modify the create
method in the RegisterController
to include the custom field in the user creation process.
If you added custom fields to the users table, make sure they are fillable in the User
model located at app/Models/User.php
. Add them to the $fillable
array to allow mass assignment.
Finally, test the registration form to ensure that the custom fields are being properly validated and saved to the database.
By following these steps, you can easily add custom fields to the registration form generated by Laravel Breeze and handle them in your application.