Skip to content
Advertisement

Laravel – Insert Selected Row

I have a table :

header 1 header 2 button
First row insert 
Second row insert
Third row insert

I want when the insert button is clicked, the row with that button is inserted in the database. So far I have in the controller :

function submitData(Request $request) {

        $data = new Data;
        $data->detail = $request->input('ft');
        $data->user_id = Auth::user()->id;
        $data->created_at = date('Y-m-d H:i:s');
        $data->save();

And in blade :

<form id="regForm" action="{{route('sendDate')}}" method="post" >
    @csrf
<tbody id="myTable">
@foreach($features as $feature)
<tr>
<td>{{ $feature->detail }}</td>
<input type="hidden" class="h-form-control" name="ft" value="{{ $feature->detail }}" >
<td>
<button type="submit" form="regForm" > insert </button>
</td>
</tr>
@endforeach

But the last row is inserted whatever button is clicked..

Advertisement

Answer

You are not using an array so all your previous inputs will be replaced by the last one. If you don’t have any more fields to send with your request you can solve this by putting your form inside your loop. button tag also accepts a value so you can skip the hidden input field to use less code.

@foreach($features as $feature)
<tr>
    <td>{{ $feature->detail }}</td>
    <td>row</td>
    <td>
        <form action="{{route('sendDate')}}" method="post" >
            @csrf
            <button type="submit" name="ft" value="{{ $feature->detail }}" > insert</button>
        </form>
    </td>
</tr>
@endforeach
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement