laravellaravel-10laravel-jetstream

Laravel/Jetstream: dynamically filled dropdown list in registration form


I have watched several videos and read some related tutorials about customization of the register form of Laravel/JetStream registration system. All I found is something like this:

//resources/views/auth/register.blade.php

<div class="mt-4">
    <x-label for="gender" value="{{ __('Gender') }}" />
    <x-dropdown id="gender" class="block mt-1 w-full" name="gender" :value="old('gender')" required>
        <x-slot name="content">
            <option value="0">Select Gender</option>
            <option value="1">Female</option>
            <option value="2">Male</option>
        </x-slot>
    </x-dropdown>
</div>

However I want to populate the genders dropdown from a database table, say "genders".

For this I have created a model Genders.

All I need to do is adding the following line

//this line will be in a file but which one?
$genders = Genders::all();

Then I will be able to list the items in the view, as follows:

<div class="mt-4">
    <x-label for="gender" value="{{ __('Gender') }}" />
    <x-dropdown id="gender" class="block mt-1 w-full" name="gender" :value="old('gender')" required>
        <x-slot name="content">
            <option value="0">Select Gender</option>
            @foreach ($genders as $gender)
                <option value="{{$gender->id}}">{{$gender->name}}</option>
            @endforeach
        </x-slot>
    </x-dropdown>
</div>

I have added the following code into app/Providers/FortifyServiceProvider.php

<?php

namespace App\Providers;

use App\Actions\Fortify\CreateNewUser;
use App\Actions\Fortify\ResetUserPassword;
use App\Actions\Fortify\UpdateUserPassword;
use App\Actions\Fortify\UpdateUserProfileInformation;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Laravel\Fortify\Fortify;
use App\Models\Gender;

class FortifyServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //I have added this but no change ):

        Fortify::registerView(function () {
            $genders = Gender::all();
            return view('auth.register', ['genders' => $genders]);
        });
    }

I could not find where should I put the code $genders = Genders::all(); and pass $genders to view register.blade.php? This could simple for a few options but what if I want to list countries for example?


Solution

  • I never knew it was so easy:

    I have added

            //
        Fortify::registerView(function () {
            $genders = Gender::all();
            return view('auth.register', ['genders' => $genders]);
        });
    

    into boot() function in app/Providers/AuthServiceProvider.php

    <?php
    
    namespace App\Providers;
    
    // use Illuminate\Support\Facades\Gate;
    use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
    use Laravel\Fortify\Fortify;
    use App\Models\Gender;
    
    class AuthServiceProvider extends ServiceProvider
    {
        /**
         * The model to policy mappings for the application.
         *
         * @var array<class-string, class-string>
         */
        protected $policies = [
            //
        ];
    
        /**
         * Register any authentication / authorization services.
         */
        public function boot(): void
        {
            //
            Fortify::registerView(function () {
                $genders = Gender::all();
                return view('auth.register', ['genders' => $genders]);
            });
        }
    }
    

    Now I can see a dynamically populated dropdown list.