Open bigint opened 3 years ago
Is this a duplicate @mkwsra?
Oh yes I created it earlier https://github.com/multicaret/laravel-acquaintances/issues/44 🤣
I will work on this library as soon as I got sometime and I will implement this if I can 😍
Thanks, we are using this library on https://taskord.com that has around 4K interactions (like, subscribe, follow etc)
Thanks for making this @mkwsra 🙏🏻
@bigint upon diggin the package for a week because I like it very much. I can suggest this.
Add it as a user model scope:
return $builder->when($hasUserAuthModel, function ($query) use ($model) {
$table = $model->getTable();
$column = $model->interactionColumn;
$modelClass = get_class($model);
// Default will always be a model/table id column if interactionColumn is not set on model.
$interactionColumn = is_null($column) ? "{$table}.id" : "{$table}.{$column}";
return $query->addSelect([
'did_interact' => InteractionRelation::selectRaw('count(*)')
->from('interactions as is_following_interactions')
->whereIn('is_following_interactions.relation', [
Interaction::RELATION_FOLLOW,
Interaction::RELATION_SUBSCRIBE
... // Depends on what you used interaction
])
->where(function ($query) use ($interactionColumn) {
return $query->whereColumn('is_blocked_interactions.subject_id', $interactionColumn)
->where('is_blocked_interactions.user_id', user()->id);
})
->whereColumn('subject_id', 'users.id')
->take(1),
])
->withCasts([
'did_interact' => 'boolean'
]);
});
I wrapped it with when()
so it will only be avaiable when there is an authticated user and it always be available when you use User model and relation with user model instance.
so for example getting the followers:
$user->followers()->havingRaw('did_interact > ?', [0])->get();
or adding a new relation since you have the pacakges traits.
// On the trait
/**
* Return followers.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function followers()
{
return $this->morphToMany(Interaction::getUserModelName(), 'subject',
config('acquaintances.tables.interactions'))
->wherePivot('relation', '=', Interaction::RELATION_FOLLOW)
->withPivot(...Interaction::$pivotColumns)
->using(Interaction::getInteractionRelationModelName())
->withTimestamps();
}
And in your user model
public function interactedFollowers()
{
return $this->followers()->havingRaw('did_interact > ?', [0]);
}
and now you can work with.
User::with(['interactedFollowers'])
->withCount(['interactedFollowers'])
->where('id', 1)
->first();
hope this helps!!
I made a also a workound using this because I cant use the isFollowedBy()
and isFollowing
on a resource since it uses the model relations exists()
which is the Interaction::isRelationExists($this, 'followings', $target, $class);
It would be great if this package support followers you know thing!