Level 13
or maybe it would be even better to add a global scope
class Reply
{
/**
* The "booted" method of the model.
*
* @return void
*/
protected static function booted()
{
static::addGlobalScope('notIgnored', function ($builder) {
return $builder->whereNotIgnored();
});
}
public function scopeWhereNotIgnored($query)
{
if (!auth()->check()) {
return $query;
}
$ignoredUserIds = auth()->user()->ignoredUsers()->pluck('users.id');
return $query->whereNotIn("{$this->getTable()}.user_id", $ignoredUserIds);
}
}