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

tomatoblob's avatar

Laravel, cant fill empty table

I am trying to fill my empty table with data based on selected value using drop-down although my code is running and does not show any error my table remains empty.

create.blade.php

<select name="course_id" id="course" class="form-control">
      @foreach($courses as $id => $course) 
            <option value="{{ $id }}" {{ (isset($applicant) && $applicant->course ? $applicant->course->id : old('course_id')) == $id ? 'selected' : '' }}>
                        {{ $course }}
             </option>
       @endforeach
</select>

Script

<script>
    $(function(){
       $('#course').change(function() {
            $.ajax({
                url: 'applicants/create/{id}',
                type: 'GET',
                data: { id:  $(this).val() },
                success: function(response)
                {
                    $('#subjectData tbody').html(response);
                }
            });
       });
    });    
</script>

Controller

public function getSubject($id)
    {
        
        $result = Subject::all()->where('course_ID', $id);

        foreach($result as $row)
        {
            $html =
                '<tr>
                    <td>' . $row->subj_code . '</td>' .
                    '<td>' . $row->subj_code . '</td>' .
                '</tr>';
        }
        return $html;
        }

Route

Route::group(['prefix' => 'admin', 'as' => 'admin.', 'namespace' => 'Admin', 'middleware' => ['auth']], function () {

    Route::get('applicants/create/{id}', 'ApplicantController@getSubject');

    Route::resource('applicants', 'ApplicantController');
   
});
0 likes
18 replies
AntLusher's avatar

I would debug the Ajax. Sounds like it's posting nothing. I guess you should do error checking. Spin open your browser inspection tools. See if you're geting any errors

AntLusher's avatar

Is that blade syntax correct? What does the DOM tree look like?

flightsimmer668's avatar

@tomatoblob In your AJAX request, try double curly braces for the id variable:

$.ajax({
          url: 'applicants/create/{{ $id }}',
          type: 'GET',
          data: { id:  $(this).val() },
          success: function(response)
          {
               $('#subjectData tbody').html(response);
          }
      });
tomatoblob's avatar

it says "Use of undefined constant id - assumed 'id' (this will throw an Error in a future version of PHP)"

AntLusher's avatar

Hard code a value first In you end point. Wait, shouldnt you be posting and not getting?

Snapey's avatar

change

        $result = Subject::all()->where('course_ID', $id);

to

        $result = Subject::where('course_ID', $id)->get();

in this loop

        foreach($result as $row)
        {
            $html =
                '<tr>
                    <td>' . $row->subj_code . '</td>' .
                    '<td>' . $row->subj_code . '</td>' .
                '</tr>';
        }
        return $html;
        }

you overwrite $html every time around the loop. You need to use $html += to concatenate to the variable.

tomatoblob's avatar

@snapey yes i was using GET, controller works fine, it shows error 404 page not found. i think im routing my controllers the wrong way and i can't seem to correct it.

EDIT: I've implemented your changes but it still showed the same error.

AntLusher's avatar

I've always taken the hardcode approach first, then once I can insure everything is linking up, then I would do the logic.

Step through from the start - make sure the Controller is getting a value, then the route, then the POST, and then the handler thats receiving the POST data. I can't see your update table SQL logic?!

Snapey's avatar

@antlusher The empty table that needs filling is an HTML one. This is not about storing data

Snapey's avatar

controller works fine, it shows error 404 page not found.

Somewhat of a contradiction?

flightsimmer668's avatar

Try this:

$.ajax({
          url: '/applicants/create/' + $(this).val(),
          type: 'GET',
          success: function(response)
          {
               $('#subjectData tbody').html(response);
          }
      });

I added a /to the url applicants/create, removed the , after 'applicants/create/', and the data: object

Snapey's avatar

Look. Forget the browser and ajax

Concentrate on getting the route working and the html returned that you expect FIRST

Your route should respond to

http://127.0.0.1:8000/admin/applicants/create/2

provided you have a subject with ID of 2

If this gives you 404, check your routes with php artisan route:list

1 like

Please or to participate in this conversation.