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

t9dev's avatar
Level 2

Send Multi checkbox and Dropdown to Controller

Hello Laracasts friends, I have searched in the wilds for days for a solution. I have a list of student that are imported using excel and I want to assign certain students to one supervisor based on Post Code ( 1 to M ) , the students data is displayed in a table with a checkbox and the supervisor's in a drop-down list.

So, I want to check one or many student and assign to supervisor.

What I want is : 1 - How to send multi checkboxes. 2 - How to update the supervisor_id only

Model\Student
public function Supervisor()
    {
        return $this->belongsTo(Supervisor::class, 'Supervisor_id');
    }
Model\Supervisor
public function Student()
    {
        return $this->hasMany(Students::class, 'id');
    }

Form Blade view

<form class="row g-3 pt-3" action="{{student.update, //what should be submitted ??}}" method="POST" enctype="multipart/form-data">
                @csrf
                @method('GET')
                            <select class="form-select" name="svName" aria-label="Default select example"> 
                                <option selected>Select a supervisor</option>
                                    @foreach ($supvervisorsList as $sv)
                                        <option value="{{$sv->id}}">{{$sv->name}}</option>
                                    @endforeach 
                            </select>   
                        </div>
                            
                        <div class="card-body">
                            <div class=" card-content table-responsive">

                                <table id="student_t" class="table table-hover table-bordered text-nowrap" style="width:100%">

                                    <thead>
                                        
                                        <th><input id="checkAllRows" type="checkbox" class="form-check-input check" /></th>
                                        <th>#</th>
                                        <th>No_Matrik</th>
                                        <th>No_KP</th>
                                        <th>Nama</th>
                                        <th>Poskod</th>
                                       
                                    </thead>
                                    

                                    <tbody>

                                    @if(!empty($state) && $state->count())
                                            @php
                                            $rowNumber = 1;
                                            @endphp
                                        @foreach($state as $row)
                                        
                                            <tr>
                                                
                                                <td> <input class="form-check-input check" type="checkbox" value="{{$row->id}}" id="CheckedRow" name="ids"></td>
                                                <td>{{$rowNumber++}}</td>
                                                <td>{{ $row->No_Matrik }}</td>
                                                <td>{{ $row->No_KP }}</td>
                                                <td>{{ $row->Nama }}</td>
                                                <td>{{ $row->Poskod }}</td>
                            
                                            </tr>

                                        @endforeach

Controller

public function viewAllocation(Request $request)
    {
        $supvervisorsList = Supervisor::all('id', 'name');
        $Negeri = $request->Negeri;
        $state = Students::where('Negeri', '=', $Negeri)
            ->whereNull('Supervisor_id')
            ->orderBy('Poskod', 'desc')
            ->get();

        $allocatedStudents = Students::where('Negeri', '=', $Negeri)
            ->with('supervisor')
            ->whereNotNull('Supervisor_id')
            ->orderBy('Poskod', 'desc')
            ->get();


        return view('admin.allocation', compact('state', 'Negeri', 'allocatedStudents', 'supvervisorsList'));
    }
0 likes
5 replies
jlrdw's avatar

Sounds like you need a checkbox array, but consider old data as well. And why do you have:

@method('GET')

For a post form?

t9dev's avatar
Level 2

@jlrdw , yes. How to do that? For the GET , I was trying to pass it to the creat() method.

Snapey's avatar

Don't use GET. Use POST if you are sending to the server.

You need to tell the checkbox that it is part of an array by adding [] eg name="ids[]"

element ids should be UNIQUE per page so you need to remove the ID on each checkbox

2 likes
t9dev's avatar
t9dev
OP
Best Answer
Level 2

Thanks to @jlrdw & @snapey .

I got the idea and solved it.

public function store(Request $request)
    {
        $request->validate([
            'svName' => 'required',
            'ids' => 'required'
        ]);



        foreach ($request->input('ids') as $student) {
            $sv = Students::find($student);
            $sv->supervisor_id = request('svName');
            $sv->save();
        }
        return back();
    }

Please or to participate in this conversation.