Reputation: 554
I am trying to update the Ticket(SET Deleted = TRUE
because I also have to show deleted Tickets). For that I am using POST
method on form with the Ticket ID on it but when form submits that Ticket ID changed to the latest Ticket ID.
When I inspect I see the exact Ticket ID in the action but when submitting, it get change.
I also tried to pass it a hidden input but unfortunately it get change to latest Ticket as well.
Blade
<tbody>
@foreach ($Tickets as $Ticket)
<tr>
<td>{{$loop->iteration}}</td>
<td>{{$Ticket->Customer->Name}}</td>
<td>$ {{$Ticket->Paid}}</td>
<td>$ {{$Ticket->TDisc}}</td>
<td>{{$Ticket->Tax}}</td>
<td>{{ date("d M Y", strtotime($Ticket->Date))}}</td>
<td class="text-right">
<a href="/Ticket/{{$Ticket->ID}}">EDIT</a>
<a href="" onclick="event.preventDefault(); document.getElementById('Delete').submit();">DELETE</a>
<form id="Delete" action="Ticket/{{$Ticket->ID}}" method="POST" style="display: one;">
<input type="hidden" name="ID" value="{{{{$Ticket->ID}}}}"
@csrf
</form>
</td>
</tr>
@endforeach
</tbody>
Controller
public function destroy(Request $request, $id) {
dd($id);
Ticket::where('ID', $id)->update(['Deleted' => TRUE]);
return redirect()->back()->with('success', 'Ticket has been Deleted');
}
Kindly help me figure out what I'm missing.
Any help would be much valued.
Upvotes: 1
Views: 42
Reputation: 3128
The reason it's not working is that your code will always target the first instance, as the id
of each form is the exact same.
You shouldn't set the same id
for any element more than once.
A better way to do it would be something like below, which will submit the correct form (the one containing the <a>
tag).
You are also missing the method field to tell laravel it's a delete request.
<form action="Ticket/{{$Ticket->ID}}" method="POST">
<input type="hidden" name="ID" value="{{$Ticket->ID}}">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<a href="" onclick="event.preventDefault(); this.parentNode.submit();">
DELETE
</a>
</form>
Upvotes: 1