matthias screed
matthias screed

Reputation: 57

Problem with Livewire and HTTP POST request in a Livewire component (GET method not supported for POST route)

I’m facing an issue when trying to send data via a Livewire component using a POST request to a specific Laravel route.

Here’s the context:

I want to send data to the route meeting.create via a POST request when the user submits a form in a Livewire component.
However, I’m getting the following error:
The GET method is not supported for route meeting. Supported methods: POST.
This indicates that the route expects a POST request, but a GET request is being sent instead.

Code used: Livewire Component:

use Illuminate\Support\Facades\Http;
use Livewire\Component;

class ReserveDate extends Component
{
    public $tracking_number;
    public $selectedDate;
    public $selectedTime;

    public function submitForm()
    {
        // Validate data
        $this->validate();

        try {
            // Send POST request to the Laravel controller method
            $response = Http::post(route('meeting.create'), [
                'tracking_number' => $this->tracking_number,
                'date' => $this->selectedDate,
                'time' => $this->selectedTime,
            ]);

            if ($response->successful()) {
                session()->flash('success', 'Appointment created successfully.');
                return redirect()->route('demarches.annuaire');
            } else {
                session()->flash('error', 'An error occurred while creating the appointment.');
            }
        } catch (\Exception $e) {
            session()->flash('error', 'Error: ' . $e->getMessage());
        }
    }
}

form view:

<form wire:submit.prevent="submitForm">
    <input type="text" wire:model="tracking_number" placeholder="Tracking Number">
    <input type="date" wire:model="selectedDate" placeholder="Date">
    <select wire:model="selectedTime">
        <option value="08:00">08:00</option>
        <!-- Other options -->
    </select>
    <button type="submit">Confirm</button>
</form>

Problem:

When I submit the form, I get a 405 Method Not Allowed error because the route meeting.create expects a POST request, not a GET request.
I suspect there might be an issue with how I’m calling the submitForm method in Livewire.

What I’ve tried:

Using Http::post() inside submitForm to send the POST request to the Laravel route.
Attempting to redirect to another route after the successful POST request.

Even after trying these approaches, I haven’t been able to resolve the issue. How can I ensure that the request is being sent as a POST and that the route meeting.create is receiving a POST request as expected?

i use lararvel 11 livewire 3 volt 1

Thanks in advance for your help!

Upvotes: 1

Views: 165

Answers (0)

Related Questions