Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

devhub-az/devhub

24 stars
3 code files
View devhub-az/devhub on GitHub

app/Helpers/HasAuthor.php

Open in GitHub
use App\Models\User;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
 
trait HasAuthor
{
public function author(): BelongsTo
{
return $this->creator();
}
 
public function authoredBy(User $author)
{
$this->creator()->associate($author);
}
 
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'author_id');
}
 
public function isAuthoredBy(User $user): bool
{
return $this->author()->matches($user);
}
}

app/Models/Comment.php

Open in GitHub
use App\Helpers\HasAuthor;
use Illuminate\Database\Eloquent\Model;
 
final class Comment extends Model
{
use HasAuthor;
//
}

app/Jobs/CreateComment.php

Open in GitHub
use App\Models\Comment;
use App\Models\User;
 
final class CreateComment
{
//
public function handle(): Comment
{
$comment = new Comment(
[
'commentable_type' => 'articles',
'commentable_id' => $this->article_id,
'body' => $this->comment,
]
);
$comment->authoredBy($this->author);
$comment->save();
 
return $comment;
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.