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

honeyBear's avatar

How to store select option input type in form

Right now, I am trying to store value from select option form into my database, how can I achieve this? And are they any difference if I'm using type enum or integer in schema for input form select option?

(create.blade.php)

<div class="form-group">
     <label>Type</label>
     <select class="form-control" name="type">
         <option selected>Pick Type</option>
         <option value="swiss">Swiss System</option>
         <option value="single">Single Elimination</option>
         <option value="double">Double Elimination</option>
     </select>
     <small class="form-text text-muted">Your tournament type</small>
 </div>

 <div class="form-group" name="size">
      <label>Participant</label>
      <select class="form-control">
           <option selected>Pick Size</option>
           <option value="2">2</option>
           <option value="4">4</option>
           <option value="8">8</option>
           <option value="16">16</option>
           <option value="32">32</option>
      </select>
      <small class="form-text text-muted">Your tournament size</small>
</div>

(postcontroller@store)

    public function store(Request $request)
    {
        $this->validate($request, [
            'type' => 'required',
            'size' => 'required',
        ]);
        
        $post = new Post();
        $post->user_id = auth()->user()->id;
        $post->type = $request->input('type');
        $post->size = $request->input('size');
        $post->save();
        
        return redirect('/posts')->with('success', 'Post Created');
    }

(schema)

Schema::create('posts', function (Blueprint $table) {
   $table->bigIncrements('id');
   $table->unsignedBigInteger('user_id');
   $table->string('type');
   $table->string('size');
   $table->timestamps();
   $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
0 likes
6 replies
Nakov's avatar
Nakov
Best Answer
Level 73

You are putting the name attribute to the div it should be to the select element:

<select class="form-control" name="size">

And for the field type in the Database it does not matter much.

bobbybouwmann's avatar

You need to give your select element a name and not the div

<div class="form-group">
    <label>Participant</label>
    <select class="form-control" name="size">
        <option selected>Pick Size</option>
        <option value="2">2</option>
        <option value="4">4</option>
        <option value="8">8</option>
    </select>
    <small class="form-text text-muted">Your tournament size</small>
</div>
honeyBear's avatar

ohhh damnit, didn't notice that. what a silly mistake. thank you for pointing that out guys. :S

honeyBear's avatar

ohhh damnit, didn't notice that. what a silly mistake. thank you for pointing that out :S ahh i see, is there any specific case if I were to use enum?

Nakov's avatar

Based on your migration you use string at the moment for both columns. I wouldn't use enum if I know that the values can change. For a specific list of values I have used it before. So, I don't know what do you mean by a specific case here.

Please or to participate in this conversation.