hossein emami
hossein emami

Reputation: 286

how to add a new input as a dropdown type and with filled default options to laravel jetstream registration form

my need is to add a new input as a dropdown type and with filled default options to laravel.8 jetstream registration form. my problems are:

  1. how to add dropdown input to the registration blade file? use jetstream component or have to create a new component based on livewire or pure laravel component?
  2. where default options for dropdown must be prepare and sent to the blade file?
  3. how can I get selected options from users in app/Actions/Fortify/CreateNewUser.php? thanks

Upvotes: 2

Views: 1711

Answers (1)

Andrew
Andrew

Reputation: 1833

I understand this is not a full answer to all of your questions but should help.

  1. You can just add a standard select, something like this:
<label for="cars">Choose a car:</label>

<select name="cars" id="cars">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>
  1. You can get/use that value in app/Actions/Fortify/CreateNewUser.php accessing the array. The following inserts the new value into the database:
return User::create([
            'name' => $input['name'],
            'email' => $input['email'],
            'car' => $input['cars']),
            'password' => Hash::make($input['password']),
            'api_token' => Str::random(60),
        ]);

You will also need to update the app/Models/User.php Model and add the new value as fillable (and of course have added the field to your User Model via a migration):

protected $fillable = [
        'name',
        'email',
        'password',
        'car',
    ];

Upvotes: 1

Related Questions