At the most basic understanding, I’ve been trying to match the route and the form action. I think that I am doing it right but I wonder why the error keeps on showing. I may have missed something anywhere but I just really couldn’t find it. Please help. With a very tight schedule, I need to complete this project.
When I submit the form it goes to this address.
index.blade.php
<tbody>
@foreach($payments as $payment)
<tr>
<td>{{ $payment->order->first_last_name }}</td>
<td>{{ $payment->order->business_name }}</td>
<td>{{ $payment->order->business_type }}</td>
<td>{{ $payment->order->phone_number }}</td>
<td>{{ $payment->order->postal_code }}</td>
<td>{{ $payment->order->email }}</td>
<td>{{ $payment->order->address }}</td>
<button type="button" class="btn btn-success btn-sm" data-action="{{ route('payments.send', $payment->id) }}" data-bs-toggle="modal" data-bs-target="#send" data-id="{{ $payment->id }}">اSend</button>
</td>
<td>
@php $status = $payment->status @endphp
<span class="{{ $status['bs_class'] }}">
{{ $status['label'] }}
</span>
</td>
<td>{{ $payment->accounting_code }}</td>
</tr>
@include('Admin.layouts.modal')
@endforeach
</tbody>
js
<script>
$('#send').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var action = button.data('action');
var orderId = button.data('id');
var modal = $(this);
modal.find('form').attr('action', action);
document.getElementById("orderId").value = orderId;
});
</script>
modal.blade.php
<div class="modal fade" id="send" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="send" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form action="{{ route('payments.accounting', $payment->id) }}" method="post">
@csrf
@method('PUT')
<input type="hidden" id="orderId" value="">
<div class="modal-header">
<h5 class="modal-title" id="sendTitle">send</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="accounting_code" class="form-label">کد accounting_code</label>
<input type="text" class="form-control" id="accounting_code" name="accounting_code">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success">save</button>
</div>
</form>
</div>
</div>
</div>
web.php
Route::put('/payments/accounting/{payment}', [AppHttpControllersAdminPaymentController::class,'accounting'])->name('payments.accounting');
PaymentController.php
public function accounting(Payment $payment, Request $request)
{
dd('hello');
$data = [
'accounting_code' => $request->post('accounting_code')
];
$payment->update($data);
return back();
}
2
Answers
Did you try it without naming the route?
delete this:
and then in HTML form:
I think you are mixing POST, PUT and GET Request here. Make sure you send your form with the correct method.
Because your js code
modal.find('form').attr('action', action)
replaced the form action when showing the modal. Try to change thedata-action
from routepayments.send
topayments.accounting
for the send button: