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

ErikRobles's avatar

Text area only inputting first letter into db Laravel 8

Hello. I have a form in which I am collecting performance data and a teacher can add as many students as he or she wants so I am collecting the data in array. My only problem is with the textarea in the fact that I am mixing up how to work with arrays and strings and now I only get the first letter of the entries in the database. This makes sense with the way I am doing it but really... I need to know how to do it right ha ha. If anyone can help me out with this, I would be grateful. Thank you. my Store function is as follows:

public function PerformanceStore(Request $request) {
        $countstudent = count($request->student_id);
        if($countstudent !=NULL) {
            for($i=0; $i < $countstudent; $i++) { 
                $perform = new Performance();
                $perform->teacher_id = Auth::user()->id;
                $perform->date = $request->date;
                $perform->last_unit_covered = $request->last_unit_covered[$i];
                $perform->student_id = $request->student_id[$i];
                $perform->last_page_viewed = $request->last_page_viewed[$i];
                $perform->exam_type = $request->exam_type[$i];
                $perform->exam_score = $request->exam_score[$i];
                $perform->comments = $request->comments[$i];
                $perform->save();
            }
        } 
        $notification = array(
            'message' => 'Students Performance Successfully Submitted.',
            'alert-type' => 'success'
        );
        return Redirect()->back()->with($notification);
    }

And my Add blade is this:

@extends('admin.admin_master')
@section('admin')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<div class="content-wrapper p-3">
    <div class="col-md-12">
        <!-- general form elements -->
        <div class="card card-primary">
          <div class="card-header">
            <h3 class="card-title">Add Performance</h3>
          </div>
          <!-- /.card-header -->
          <!-- form start -->
          <form action="{{ route('performance_store') }}" method="post">
            @csrf
            <div class="card-body">
                <div class="row">
                    <div class="col-md-12">
                        
                        <div class="form-group">
                            <label for="teacher">Teacher's Name</label>
                            <input class="form-control" type="text" name="teacher_id" value="{{ Auth::user()->name; }}">
                        </div> 
                    </div>
                
                    <div class="col-md-12">
                        <div class="form-group">
                            <label for="date">Select Date</label>
                            <input class="form-control" type="date" value="{{ date('Y-m-d') }}" name="date" required>  
                        </div>
                    </div><!--End col md 12-->
                </div><!--end row-->
                <div class="add_item">

                <div class="row">
                    <div class="col-md-3">
                        <div class="form-group">
                          <h5>Student Name <span class="text-danger">*</span></h5>
                          <div class="controls">
                              <select name="student_id[]" required class="form-control" id="student_name">
                                  <option value="" selected disabled>Select Student Name</option>
                                  @foreach($students as $student)
                                      <option value="{{ $student->id }}">{{ $student->name }}</option>
                                  @endforeach
                              </select>
                          </div>
                        </div>
                          {{-- End form Group --}}    
                      </div>
                      {{-- End Col md 3 --}}

                      <div class="col-md-3">
                        <div class="form-group">
                          <h5>Last Unit Covered </h5>
                          <div class="controls">
                              <select name="last_unit_covered[]" class="form-control">
                                  <option value="" selected disabled>Last Unit Covered</option>
                                  <option value="1">1</option>
                                  <option value="2">2</option>
                                  <option value="3">3</option>
                                  <option value="4">4</option>
                                  <option value="5">5</option>
                                  <option value="6">6</option>
                                  <option value="7">7</option>
                                  <option value="8">8</option>
                                  <option value="9">9</option>
                                  <option value="10">10</option>
                                  <option value="11">11</option>
                                  <option value="12">12</option>
                                  <option value="13">13</option>
                                  <option value="14">14</option>
                                  <option value="15">15</option>
                                </select>
                          </div>
                        </div>
                          {{-- End form Group --}}    
                      </div>
                      {{-- End Col md 3 --}}

                      <div class="col-md-3">
                        <div class="form-group">
                          <h5>Last Page Viewed <span class="text-danger">*</span></h5>
                          <div class="controls">
                            <input name="last_page_viewed[]" type="number" class="form-control" placeholder="Enter page number">                            
                          </div>
                        </div>
                          {{-- End form Group --}}    
                      </div>
                      {{-- End Col md 3 --}}

                      <div class="col-md-3">
                        <div class="form-group">
                          <h5>Exam Type </h5>
                          <div class="controls">
                              <select name="exam_type[]" class="form-control">
                                  <option value="" selected disabled>Select Exam Type</option>
                                  <option value="End of Unit Review">End of Unit Review</option>
                                  <option value="Progress Test 1-3">Progress Test 1-3</option>
                                  <option value="Progress Test 4-6">Progress Test 4-6</option>
                                  <option value="Progress Test 7-9">Progress Test 7-9</option>
                                  <option value="Progress Test 10-12">Progress Test 10-12</option>
                                  <option value="Progress Test 13-15">Progress Test 13-15</option>
                                  <option value="Exit Test">Exit Test</option>
                                </select>
                          </div>
                        </div>
                          {{-- End form Group --}}    
                      </div>
                      {{-- End Col md 3 --}}

                      <div class="col-md-4">
                        <div class="form-group">
                          <h5>Exam Score </h5>
                          <div class="controls">
                            <input name="exam_score[]" type="number" class="form-control" placeholder="Enter Exam Score">                            
                          </div>
                        </div>
                          {{-- End form Group --}}    
                      </div>
                      {{-- End Col md 4 --}}

                      <div class="col-md-6">
                        <div class="form-group">
                          <h5>Comments </h5>
                            <textarea class="form-control" name="comments[]" placeholder="Add Comments about your student's progress here."></textarea>  
                        </div>
                    </div><!--End col md 6-->
                      
                      <div class="col-md-2" style="padding-top: 25px;">
                        <span class="btn btn-success addeventmore" title="Add another student progress report"><i class="fa fa-plus-circle"></i></span>
                      </div>
                      
                </div><!--End Row-->
                <br> <br>
            </div><!--End add_item row-->
            
            </div>
            <!-- /.card-body -->


            <div class="card-footer">
              <button type="submit" class="btn btn-primary">Submit Progress Report</button>
            </div>
          </form>
        </div>
        <!-- /.card -->

    </div>
</div>
</div>

<div style="visibility: hidden;">
    <div class="whole_extra_item_add" id="whole_extra_item_add">
      <div class="delete_whole_extra_item_add" id="delete_whole_extra_item_add">
        <div class="form-row">
         
          <div class="col-md-3">
            <div class="form-group">
              <h5>Student Name <span class="text-danger">*</span></h5>
              <div class="controls">
                  <select name="student_id[]" required class="form-control" id="student_name">
                      <option value="" selected disabled>Select Student Name</option>
                      @foreach($students as $student)
                          <option value="{{ $student->id }}">{{ $student->name }}</option>
                      @endforeach
                  </select>
              </div>
            </div>
              {{-- End form Group --}}    
          </div>
          {{-- End Col md 3 --}}

          <div class="col-md-3">
            <div class="form-group">
              <h5>Last Unit Covered </h5>
              <div class="controls">
                  <select name="last_unit_covered[]" class="form-control">
                      <option value="" selected disabled>Last Unit Covered</option>
                      <option value="1">1</option>
                      <option value="2">2</option>
                      <option value="3">3</option>
                      <option value="4">4</option>
                      <option value="5">5</option>
                      <option value="6">6</option>
                      <option value="7">7</option>
                      <option value="8">8</option>
                      <option value="9">9</option>
                      <option value="10">10</option>
                      <option value="11">11</option>
                      <option value="12">12</option>
                      <option value="13">13</option>
                      <option value="14">14</option>
                      <option value="15">15</option>
                    </select>
              </div>
            </div>
              {{-- End form Group --}}    
          </div>
          {{-- End Col md 3 --}}

          <div class="col-md-3">
            <div class="form-group">
              <h5>Last Page Viewed <span class="text-danger">*</span></h5>
              <div class="controls">
                <input name="last_page_viewed[]" type="number" class="form-control" placeholder="Enter page number">                            
              </div>
            </div>
              {{-- End form Group --}}    
          </div>
          {{-- End Col md 3 --}}

          <div class="col-md-3">
            <div class="form-group">
              <h5>Exam Type </h5>
              <div class="controls">
                  <select name="exam_type[]" class="form-control">
                      <option value="" selected disabled>Select Exam Type</option>
                      <option value="End of Unit Review">End of Unit Review</option>
                      <option value="Progress Test 1-3">Progress Test 1-3</option>
                      <option value="Progress Test 4-6">Progress Test 4-6</option>
                      <option value="Progress Test 7-9">Progress Test 7-9</option>
                      <option value="Progress Test 10-12">Progress Test 10-12</option>
                      <option value="Progress Test 13-15">Progress Test 13-15</option>
                      <option value="Exit Test">Exit Test</option>
                    </select>
              </div>
            </div>
              {{-- End form Group --}}    
          </div>
          {{-- End Col md 3 --}}

          <div class="col-md-4">
            <div class="form-group">
              <h5>Exam Score </h5>
              <div class="controls">
                <input name="exam_score[]" type="number" class="form-control" placeholder="Enter Exam Score">                            
              </div>
            </div>
              {{-- End form Group --}}    
          </div>
          {{-- End Col md 4 --}}

          <div class="col-md-6">
            <div class="form-group">
              <h5>Comments </h5>
                <textarea class="form-control" name="comments[]"></textarea>  
            </div>
        </div><!--End col md 6-->
              
          <div class="col-md-2" style="padding-top: 25px;">
            <span class="btn btn-success addeventmore"><i class="fa fa-plus-circle"></i></span>
            <span class="btn btn-danger removeeventmore"><i class="fa fa-minus-circle"></i></span>
          </div>
          
        </div>
        <br> <br>
      </div>
    </div>
  </div>

  <script>
    $(document).ready(function() {
      let counter = 0;
      $(document).on("click", ".addeventmore", function () {
        let whole_extra_item_add = $('#whole_extra_item_add').html();
        $(this).closest(".add_item").append(whole_extra_item_add);
        counter++;
      });
      $(document).on("click", ".removeeventmore", function (event) {
        $(this).closest(".delete_whole_extra_item_add").remove();
        counter -= 1;
      });
    });
  </script>
@endsection

Like I said, all the data is getting to the database. If I add 4 student's progress reports, I get 4 reports in the db. If I have comment 1 = Hello and comment two= Are and Comment Three = Love, my db shows H. R. O. One letter for each comment. Hopefully that makes sense. Obviously, I need all the comment data. Thanks again in advance.

0 likes
7 replies
orest's avatar

You have a single text area in which you expect comments for all students without specifying which comment goes to which student ? What is the connection between a comment and a student ?

ErikRobles's avatar

@orest I'll edit my question to show the entire form. Sorry, I was trying to be brief and not fill up the page with a bunch of code.

orest's avatar

@ErikRobles

yeah that's huge.

can you check first what is sent from your form ?

dd(request('comments'));
ErikRobles's avatar

@orest It brings back the first letter of the last comment made. So if my second student received the comment "More testing", my dd($request->comments); returns "M".

Snapey's avatar
Snapey
Best Answer
Level 122

your field array is named "comments" but you are dumping "comment" ?

some text areas are name="comments[]" but at least one is name="comment"

Make sure you understand the data in the request is valid before worrying about the controller

1 like
ErikRobles's avatar

@Snapey Such a silly mistake. Thank you for pointing it out to me. I fixed it and now the code works as it should. Thanks a million

Please or to participate in this conversation.