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

lrf002's avatar

How to select different registers in a table in a view in laravel?

Here´s the thing. I have a view to show different students in a table. I`d like to select some of them, and send that to a method in my controller. Here is my table in the view:

  @auth
  <h1>Añade amigos de {{$student->name}}</h1>
  <table class="table">
    <thead style="background-color:#9ED5C5">
      <tr>
        <th>Nombre</th>
        <th>Apellidos</th>
        <th>Edad</th>
        <th>Opciones</th>
      </tr>
    </thead>
    <tbody>
        @foreach($students as $student)
            <tr>
                <td>{{$student->name}}</td>
                <td>{{$student->surname}}</td>
                <td>{{$student->age}}</td>
                <td>
                </td>
            </tr>
        @endforeach
    </tbody>
  </table>
</div>

@endauth

@guest

Debes iniciar sesión

@endguest

I´d like to select some of the registers, store them in and array or something like that and send that array to a method in my controller, but I dont know how to select various rows and send them. Thank you.

0 likes
4 replies
tykus's avatar
tykus
Best Answer
Level 104

Wrap the table with a form:

<form method="post" action="{{ route('your-route') }}">
    @csrf
    <!-- table head etc. -->
        @foreach($students as $student)
            <tr>
                <td><input type="checkbox" value="{{ $student->id }}" name="selected[]"></td>
                <td>{{$student->name}}</td>
                <td>{{$student->surname}}</td>
                <td>{{$student->age}}</td>
                <td>
                </td>
            </tr>
        @endforeach
    <!-- table foot etc. -->
    <button type="submit">Submit</button>
</form>

Define the Route and Controller action where you can handle the array of student IDs

public function store(Request $request)
{
    $selectedStudents = $request->input('selected');
}
2 likes
lrf002's avatar

@tykus And that way, ian array of IDs of the selected students will be sent , won`t it?

tykus's avatar

@lrf002 just as an aside; you are overwriting the main $student variable value in the loop; you might want to consider a different variable for the loop item, e.g.

 <h1>Añade amigos de {{$student->name}}</h1>
<!-- ... -->
        @foreach($students as $otherStudent)
1 like

Please or to participate in this conversation.