1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Why PATCH Method Fails While POST Method Works in Laravel?

Discussão em 'Outras Linguagens' iniciado por achilles, Setembro 10, 2024.

  1. achilles

    achilles Guest

    I am encountering an issue when sending a PATCH request in my Laravel application. When sending a PATCH request to update a resource, I am not getting the expected result. However, when I send the same request using the POST method, I get the desired result. What could be the issue with the PATCH request?

    // web
    Route::patch('{id}/update', [UrunlerController::class, 'update'])->name('magaza.urun.guncelle.submit');


    // Controller
    public function update(Request $request, string $id)
    {
    $sayi = $request->input('sayi');

    return response()->json([
    'success' => true,
    'sayi' => $sayi
    ]);
    }


    // vanilla js
    document.getElementById('patates').addEventListener('click', function() {
    var formData = new FormData();
    formData.append('sayi', 123); // Basit veri ekliyoruz

    var url = `{{ route('magaza.urun.guncelle.submit', ['id' => 8]) }}`;

    fetch(url, {
    method: 'PATCH',
    headers: {
    'X-CSRF-TOKEN': '{{ csrf_token() }}'
    },
    body: formData
    })
    .then(response => response.json())
    .then(data => {
    console.log(data);
    })
    .catch(error => {
    console.error('Hata:', error);
    });
    });


    The result I expected

    {
    "success": true,
    "sayi": "123"
    }


    The result I encountered

    {
    "success": true,
    "sayi": null
    }


    Laravel v:11.20.0

    Continue reading...

Compartilhe esta Página