Certainly! The error:
Livewire\Exceptions\MethodNotFoundException
Unable to call component method. Public method [createComment] not found on component
usually means that Livewire cannot find a public createComment method on your component when it is triggered from the frontend.
Common Causes & Solutions
1. Method Visibility
Double check that your createComment method is indeed marked as public. From your code, it looks correct:
public function createComment()
{
// ...
}
2. Component Configuration
Check that your Livewire component is correctly referenced in your Blade file. For Livewire v3 (which uses livewire:) or @livewire, this looks like:
<livewire:comments.comment-create :post="$post" />
Or in older syntax:
@livewire('comments.comment-create', ['post' => $post])
If you have recently renamed your class or file, clear view and Livewire caches:
php artisan view:clear
php artisan livewire:discover
php artisan cache:clear
3. Event Trigger Type
How are you triggering createComment?
In your Blade, you have:
<x-button.default wire:click="createComment" type="submit">
{{ __('Send') }}
</x-button.default>
If the button is nested in a <form>, and you submit the form, it might submit traditionally (via browser) instead of triggering the Livewire method unless you use wire:submit.prevent on the <form> itself.
If this button is not in a <form> tag, or if you are using only wire:click, you are fine. If it is part of a <form>, you should:
- EITHER use
wire:submit.prevent="createComment"in the<form>tag - OR make your button type
"button", not"submit"
Example:
<form wire:submit.prevent="createComment">
<!-- textarea and buttons -->
<x-button.default type="submit">
{{ __('Send') }}
</x-button.default>
</form>
or
<x-button.default wire:click="createComment" type="button">
{{ __('Send') }}
</x-button.default>
4. Naming Consistency
Ensure your Livewire component class and file match the registration:
- File:
app/Livewire/Comments/CommentCreate.php - Class:
App\Livewire\Comments\CommentCreate
5. Version Issues
If you're using Livewire v3, make sure you have updated all use statements and attributes (like #[Validate]), and your Blade syntax follows the v3 conventions.
Summary Solution Checklist
- Ensure your
createCommentmethod ispublic. - Ensure your Livewire component's file/class name matches and is discoverable (run
php artisan livewire:discover). - Make sure the event is triggered as intended: use either
wire:clickon buttons oftype="button", orwire:submit.preventon forms. - Clear your caches after any changes.
Example Blade Adjustment
If you are in a <form>, update like so:
<form wire:submit.prevent="createComment">
<textarea ...></textarea>
<!-- Buttons ... -->
<x-button.default type="submit">
{{ __('Send') }}
</x-button.default>
</form>
Or, stick with a button and make the type explicit:
<x-button.default wire:click="createComment" type="button">
{{ __('Send') }}
</x-button.default>
Try these steps. If you're still seeing errors, ensure you:
- Have the up-to-date Livewire version
- Have not made any typos in the method name
- Registered the component correctly
Let me know if you need more help after these adjustments!