Laravel Livewire The POST method is not supported for this route

4.9k Views Asked by At

As soon as I inserted this title the system showed me all the similar questions, and none of them help me. I get this error "The POST method is not supported for this route." no matter what I try. Even worse, is I already made another component with the identical logic, and that one works good.

here are the routes: (teeoffform works, bulletin does not)

Route::get('/bulletin', function () {
    return view('bulletin');
   });
Route::get('/teeoffform', function () {
    return view('teeoffform');
});

Here are the form tags: both identical one works one doesn't

<form wire:submit.prevent="submit" method="POST">

this is my component from the one that doesn't work (bulletin) the only difference from the other one that does work, is that there is no rendering method, so I tried to take it out and see if that was the problem, but no luck... I thought, since my route is alrady calling a view maybe the conflict is there... but it doesn't matter, I get the error anyways, and I'm out of ideas.

<?php

namespace App\Http\Livewire;

use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use App\Models\Bulletins;
use App\Models\User;


class Bulletin extends Component
{
            public $title;
            public $message;
            public $messagesending;
            public $user_email;
            public $userTable_email;
            public $expires;
            public $success_message;
            protected $rules = [
                'title' => 'required',
                'message' => 'required',
                'user_email' => 'required',
                'expires' => 'required',
            ];

    public function render()
    {
        return view('livewire.bulletin', ['email_data' => User::orderBy('email','asc')->get()]);
    }

    public function submit()
    {
        $this->validate();

        $sendMessage = new Bulletins;
        $sendMessage->title = $this->title;
        $sendMessage->message = $this->messagesending;
        $sendMessage->user_email = $this->user_email;
        $sendMessage->expires = $this->expires;
        $sendMessage->save();

        $this->success_message = 'Message Sent Successfully';
    }

}

I really don't get it... I looked for 4 hours now why this is happening.

2

There are 2 best solutions below

0
On

I found the difference, not in the logic of the code, but where I was running it from. If I was testing from (localhost/bulletin) I was getting that error. if I included the component inside the dashboard (localhost/home) and ran it from there, then everything worked...

why is that? I can't go to (localhost/bulletin) without being logged in, so I was logged in.

0
On

I ran into this issue as well and found that I was not including the Livewire styles and scripts in my apps layouts blade files.

@livewireStyles @livewireScripts