Synchro back-office, fix on tariffs
This commit is contained in:
11
app/Traits/CanComment.php
Normal file
11
app/Traits/CanComment.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
trait CanComment
|
||||
{
|
||||
public function needsCommentApproval($model): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
36
app/Traits/HasComments.php
Normal file
36
app/Traits/HasComments.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Contracts\Commentator;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
use App\Models\Core\Comment;
|
||||
|
||||
trait HasComments
|
||||
{
|
||||
public function comments()
|
||||
{
|
||||
return $this->morphMany(Comment::class, 'commentable');
|
||||
}
|
||||
|
||||
public function comment(string $comment)
|
||||
{
|
||||
return $this->commentAsUser(auth()->user(), $comment);
|
||||
}
|
||||
|
||||
public function commentAsUser(?Model $user, string $comment)
|
||||
{
|
||||
|
||||
$comment = new Comment([
|
||||
'comment' => $comment,
|
||||
'is_approved' => ($user instanceof Commentator) ? ! $user->needsCommentApproval($this) : false,
|
||||
'user_id' => is_null($user) ? null : $user->getKey(),
|
||||
'commentable_id' => $this->getKey(),
|
||||
'commentable_type' => get_class(),
|
||||
]);
|
||||
|
||||
return $this->comments()->save($comment);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user