multicaret/laravel-acquaintances
Installation
composer require multicaret/laravel-acquaintances
Publish the config (optional):
php artisan vendor:publish --provider="Multicaret\Acquaintances\AcquaintancesServiceProvider" --tag="config"
Basic Usage Add traits to your Eloquent models:
use Multicaret\Acquaintances\Traits\HasFollowers;
use Multicaret\Acquaintances\Traits\HasLikes;
use Multicaret\Acquaintances\Traits\HasRatings;
class Post extends Model
{
use HasFollowers, HasLikes, HasRatings;
}
First Interaction
// Follow a user
$user->follow($post->user);
// Like a post
$post->like($currentUser);
// Rate a post (1-5 stars)
$post->rate($currentUser, 4);
config/acquaintances.php (caching, morph keys, table names)withAcquaintanceCounts()AcquaintanceCreated, AcquaintanceDeleted for custom logic// Create a friendship
$friendship = $user->becomeFriendsWith($otherUser);
// Add to a group
$group = $user->createGroup('Colleagues');
$group->addMember($otherUser);
// Check membership
if ($user->isFriendsWith($otherUser)) { ... }
// Bulk operations
$post->like([$user1, $user2]);
$post->unlike([$user1]);
// Check interactions
if ($post->isLikedBy($user)) { ... }
// Pagination with counts
$posts = Post::withAcquaintanceCounts()->paginate(10);
// Rate with metadata
$post->rate($user, 5, ['comment' => 'Great post!']);
// Get average rating
$average = $post->averageRating();
// Filter by rating
$topPosts = Post::ratedAbove(4)->get();
// Verify a user
$verifier->verify($user, 'email');
// Check verification
if ($user->isVerified('email')) { ... }
'cache' => [
'enabled' => env('ACQUAINTANCES_CACHE_ENABLED', true),
'driver' => 'redis',
],
'morphs' => [
'user' => 'users',
'post' => 'posts',
],
likedBy(), followedBy() in queries.Counter Cache Mismatch
increment/decrement calls match your counter columns (e.g., likes_count).HasAcquaintanceCounters trait or manually sync counters.UUID/ULID Conflicts
uuidMorphs or ulidMorphs in the config to avoid key errors.Cache Invalidation
Acquaintance::clearCacheFor($post);
Event Overhead
$this->withoutEvents(function () {
$post->like($user);
});
Acquaintance::getCachedInteractions($post);
morphMap in your models and the package config.Custom Interactions
Create a new interaction type by extending Multicaret\Acquaintances\Contracts\Interaction:
class Report implements Interaction { ... }
Register it in config/acquaintances.php under interactions.
Custom Verification Types
Extend Multicaret\Acquaintances\Contracts\Verification and add to the verifications config array.
Macros
Extend the HasAcquaintances macro for reusable query logic:
\Multicaret\Acquaintances\HasAcquaintances::macro('trending', function () {
return $this->orderByDesc('likes_count')->limit(10);
});
withAcquaintanceCounts() or with() for related models.like(), follow()) over loops for performance.decimal(3,2) for rating columns to avoid floating-point errors.How can I help you explore Laravel packages today?