beyondcode/laravel-comments

This package may conflict with Laravel Nova

davidrichied opened this issue · 2 comments

Hi,

I believe this package conflicts with Nova (the Larave admin panel). The error happens when I try to save a comment resource in Nova.

Too few arguments to function BeyondCode\Comments\Comment::comment(), 0 passed in /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php on line 411 and exactly 1 expected

It looks like the issue is with the comment method in the HasComments trait (line 28). If I change the name of the comment method from comment to addComment, then I can correctly save the comment using Nova. Nova must be getting confused somewhere because of the the name of the trait's method (comment).

A possible workaround I've come up with, and it seems to work, is replacing the comment() method in your own trait with a relationship (since Nova is assuming that it is one), and using your custom trait instead of the package's one.

We're not using the comment() method in our project (only the commentAsUser()), so we don't ”miss it”. :)


All your models:

- use BeyondCode\Comments\Traits\HasComments;
+ use App\Traits\HasComments;

app/Comment.php:

<?php

namespace App;

use BeyondCode\Comments\Comment as CommentModel;
use App\Traits\HasComments;

class Comment extends CommentModel
{
    use HasComments;
}

app/Traits/HasComments.php:

<?php

namespace App\Traits;

use BeyondCode\Comments\Traits\HasComments as BaseCommentsTrait;

trait HasComments
{

    use BaseCommentsTrait;

    public function comment(string $str = '')
    {
        return $this->commentable(); // because Nova is expecting a relationship here
    }

}

I hope this helps for everyone looking to settle for a semi-hacky solution. 😆

Nice workaround!

Thanks!