Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shoulieheinds's avatar

how to create select box with data from table?

Hello Brothers, here I have 3 tables (gender, religion, & segmentation) where table structure like this id | name 1 | Male 2 | Female

I want to call this id on select box for registration form. how to do that? thank so much for your help.

0 likes
12 replies
Snapey's avatar
Snapey
Best Answer
Level 122

In your controller, get the tables and pass to the form


$genders = Gender::all();

return view('registration')->withGenders($genders);

The in the form

<select name="gender">
    <option>Select Gender</option>
    @foreach($genders as $gender)
        <option value="{{$gender->id}}">{{$gender->name}}</option>
    @endforeach 
</select>
shoulieheinds's avatar

to get table which controller i put this? inside RegisterController or make new controller?

sujancse's avatar

You can add it to RegisterController where are you rendering your views(Registration form).

1 like
eliyas5044's avatar

you can try like this

$genders = Gender::all();

return view('registration', compact('genders'));
1 like
sujancse's avatar

You are definitely doing something wrong. Can you please show your controller and views where you adding your codes?

1 like
Snapey's avatar

Have you got it working? I thought you wanted it as part of the registration, but you have created a new view?

In RegisterController.php, add this method, then add the gender stuff to this.

    /**
     * Show the application registration form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showRegistrationForm()
    {

        $genders = Gender::all();

        return view('auth.register')->withGenders($genders);

    }

(and remove it from your create function)

This replaces the same function in the RegistersUsers trait

Then put the select in the auth/register.blade.php view

Please or to participate in this conversation.