I’m working on a Laravel application for invoice management. I’m trying to implement a feature to duplicate invoices, but I’m encountering a 404 “Resource not found” error when making the AJAX call.
Here’s my route definition:
$this->group('/electronic-invoices', function () use ($container) {
$this->post('/duplicate/{id}', InvoiceOutController::class . ':duplicate')->setName('duplicate_electronic_invoices');
});
Here’s the relevant part of my InvoiceOutController:
public function duplicate($request, $response, $args)
{
$this->logger->info('Metodo duplicate chiamato con ID: ' . $args['id']);
$invoiceId = $args['id'];
$originalInvoice = ElectronicInvoice::find($invoiceId);
if (!$originalInvoice) {
return $response->withJson(['success' => false, 'message' => 'Fattura non trovata'], 404);
}
try {
DB::beginTransaction();
$newInvoice = $originalInvoice->replicate();
$newInvoice->invoice_number = $this->generateNewInvoiceNumber();
$newInvoice->date = date('Y-m-d');
$newInvoice->save();
foreach ($originalInvoice->items as $item) {
$newItem = $item->replicate();
$newItem->invoice_id = $newInvoice->id;
$newItem->save();
}
DB::commit();
return $response->withJson(['success' => true, 'message' => 'Fattura duplicata con successo'], 200);
} catch (Exception $e) {
DB::rollBack();
return $response->withJson(['success' => false, 'message' => 'Errore durante la duplicazione: ' . $e->getMessage()], 500);
}
}
And here’s my JavaScript code that makes the AJAX call:
$('#invoices').on('click', '.duplicate-invoice', function(e) {
e.preventDefault();
var invoiceId = $(this).data('id');
$('#duplicateConfirmModal').modal('show');
$('#confirmDuplicate').off('click').on('click', function() {
$.ajax({
url: '{{ path_for('duplicate_electronic_invoices', {'id': ''}) }}' + invoiceId,
method: 'POST',
success: function(response) {
if (response.success) {
alert(response.message);
table.ajax.reload();
} else {
alert('Errore: ' + response.message);
}
},
error: function() {
alert('Si è verificato un errore durante la duplicazione.');
}
});
$('#duplicateConfirmModal').modal('hide');
});
});
When I try to duplicate an invoice, I get the following error:
[Fri Aug 2 17:22:41 2024] ::1:54565 [404]: /electronic-invoices/duplicate/51584
{"error":"Risorsa non trovata."}
I’ve tried the following:
- Verified that the route is in the correct group
- Checked for any typos in the route name
- Added logging statements to all middleware, but none of them seem to be triggered
- Verified that the controller and method exist and are accessible
- Checked for any global middleware that might be interfering
What could be causing this 404 error, and how can I resolve it to make the invoice duplication work correctly?
Bumbazzaaaa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.