skip to Main Content

I use the Livewire framework in my Laravel, I created a comment feature, I want when I comment the screen will automatically scroll to the comment via the ID that I inserted using JavaScript but when I make a comment it should automatically scroll down but instead it appears
error message "Method AppLivewirePostComment::emit does not exist."

Please help me

 public function store()
    {
        $this->validate(["body" => "required"]);
        $comment = ModelsComment::create([
            'user_id' => Auth::user()->id,
            "article_id" => $this->article->id,
            "body" => $this->body
        ]);

        if($comment){
            $this->emit('commentStored', $comment->id);
            $this->body = null; 
        } else {
            session()->flash('danger', 'Komentar gagal Ditambahkan!!');
            return redirect()->route('data', $this->article->slug);
        }
    }

$this->emit('commentStored', $comment->id);

I searched on any site but no one discussed this error

2

Answers


  1. I suppose you are using Livewire 3.
    In this version the emit() and dispatchBrowserEvent() methods have been unified into the dispatch() method.

    The parameter must now be identified with the name assigned in the event handler:

    $this->dispatch('commentStored', commentId: $comment->id);
    
    namespace AppLivewire;
    use LivewireAttributesOn; // <~~~ note this
    
    class someClass extends Component
    {
        ....
    
        #[On('commentStored')] // <~~~ and this
        public function methodThatHandlesTheEvent(int $commentId) // <~~~ parameter name
        {
           dd ($commentId);
        }
    
        ....
    
    
    Login or Signup to reply.
  2. In my case, upgrading from livewire 2.x to 3.x is easy:

    $this->emit('updateHeaderOfForm'); //livewire 2.x

    //is now

    $this->dispatch('updateHeaderOfForm'); //livewire 3.x

    NO MORE CHANGES

    The solution is in the livewire documentation linked in the first answer of this post, more precisely at https://livewire.laravel.com/docs/upgrading#events

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search