Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ class Commenter extends NovaCommenter

Then use this class instead of the default `Commenter` class within your resources.

### Customizing the comment

By default, the `Comment` model will strip tags from the comment body. If you would like to modify this behavior, you can pass an callback to the `Comment::whenCreating` method. This callback will receive the comment model instance as a parameter.

```php
Comment::whenCreating(function (Comment $comment) {
$comment->comment = strip_tags($comment->comment);
});
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
],
"require": {
"php": ">=8.0",
"laravel/nova": "^4.0"
"laravel/nova": "^4.0|^5.0"
},
"autoload": {
"psr-4": {
Expand Down
39 changes: 27 additions & 12 deletions src/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace KirschbaumDevelopment\NovaComments\Models;

use Closure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
Expand All @@ -17,35 +18,49 @@ class Comment extends Model
*/
protected $table = 'nova_comments';

/**
* @var Closure|callable|null
*/
protected static $whenCreating = null;

/**
* The "booting" method of the model.
*/
public static function boot(): void
{
parent::boot();

static::creating(
function ($comment): void {
if (auth()->check()) {
$comment->commenter_id = auth()->id();
}
static::creating(function ($comment): void {
if (auth()->check()) {
$comment->commenter_id = auth()->id();
}
);

if (static::$whenCreating) {
call_user_func(static::$whenCreating, $comment);
} else {
$comment->comment = filter_var(
strip_tags($comment->comment),
FILTER_SANITIZE_SPECIAL_CHARS
);
}
});
}

/**
* @return MorphTo
*/
public function commentable(): MorphTo
{
return $this->morphTo();
}

/**
* @return BelongsTo
*/
public function commenter(): BelongsTo
{
return $this->belongsTo(config('auth.providers.users.model'), 'commenter_id');
}

/**
* @param Closure|callable $callback
*/
public static function whenCreating($callback): void
{
static::$whenCreating = $callback;
}
}
Loading