Skip to main content

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

Read more here

laravelio/laravel.io

2497 stars
7 code files
View laravelio/laravel.io on GitHub

app/Models/Tag.php

Open in GitHub
use Illuminate\Database\Eloquent\Relations\MorphToMany;
 
final class Tag extends Model
{
public function articles(): MorphToMany
{
return $this->morphedByMany(Article::class, 'taggable');
}
}

app/Helpers/HasTags.php

Open in GitHub
use App\Models\Tag;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
 
trait HasTags
{
public function tags()
{
return $this->tagsRelation;
}
 
public function syncTags(array $tags)
{
$this->save();
$this->tagsRelation()->sync($tags);
 
$this->unsetRelation('tagsRelation');
}
 
public function removeTags()
{
$this->tagsRelation()->detach();
 
$this->unsetRelation('tagsRelation');
}
 
public function tagsRelation(): MorphToMany
{
return $this->morphToMany(Tag::class, 'taggable')->withTimestamps();
}
}

app/Models/Thread.php

Open in GitHub
use App\Helpers\HasLikes;
use App\Helpers\HasTags;
 
final class Thread extends Model
{
use HasLikes;
use HasTags;
 
public static function feedByTagPaginated(Tag $tag, int $perPage = 20): Paginator
{
return static::feedByTagQuery($tag)
->paginate($perPage);
}
 
public static function feedByTagQuery(Tag $tag): Builder
{
return static::feedQuery()
->join('taggables', function ($join) {
$join->on('threads.id', 'taggables.taggable_id')
->where('taggable_type', static::TABLE);
})
->where('taggables.tag_id', $tag->id());
}
}

app/Helpers/HasLikes.php

Open in GitHub
use App\Models\Like;
use Illuminate\Database\Eloquent\Relations\MorphMany;
 
trait HasLikes
{
public function likesRelation(): MorphMany
{
return $this->morphMany(Like::class, 'likesRelation', 'likeable_type', 'likeable_id');
}
}

app/Models/Article.php

Open in GitHub
use App\Helpers\HasLikes;
use App\Helpers\HasTags;
 
final class Article extends Model
{
use HasLikes;
use HasTags;
 
protected $with = [
'likesRelation',
];
}

app/Jobs/CreateArticle.php

Open in GitHub
use App\Models\Article;
 
final class CreateArticle
{
private $tags;
 
public function handle(): Article
{
$article = new Article([
'title' => $this->title,
'body' => $this->body,
'original_url' => $this->originalUrl,
'slug' => $this->title,
'submitted_at' => $this->shouldBeSubmitted ? now() : null,
]);
$article->syncTags($this->tags);
 
return $article;
}
}

app/Jobs/LikeArticle.php

Open in GitHub
use App\Models\Article;
 
final class LikeArticle
{
public function handle(): void
{
if ($this->article->isLikedBy($this->user)) {
throw CannotLikeItem::alreadyLiked('article');
}
 
$this->article->likedBy($this->user);
}
}

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.