Skip to main content

Liking and Disliking Content

The Laravel Like package provides a simple, transactional way to add like, dislike, and love interactions to any Eloquent model. This guide covers the core action methods — both from your model and from the Like/Love facades.

Prerequisites​

  • Installation completed
  • HasLike trait added to your content model (or HasLove for love-only surfaces)
  • User authentication set up (for automatic user resolution) — or pass an explicit user id

Available action methods​

MethodReturnDescription
like($userId?)LikeLike the model. Idempotent (no-op if already liked).
dislike($userId?)LikeDislike the model. Idempotent.
love($userId?)LikeLove the model. Idempotent.

These are available on:

  1. Any model with the HasLike or HasLove trait (via $model->like() etc.)
  2. The Like and Love facades (via Like::like($model) etc.)
  3. The LikeManager class (via app(LikeManager::class)->like($model))

All three options are equivalent. Pick the one that fits your context.

Basic usage​

From the model​

$post = Post::find(1);

// Assumes auth()->id() as the user
$post->like();
$post->dislike();
$post->love();

From the facade​

use CSlant\LaravelLike\Facades\Like;

$video = Video::find(1);

Like::like($video);
Like::dislike($video);
Like::love($video);

Explicit user id​

To interact on behalf of a different user (or when unauthenticated), pass the user id as the second argument:

$post->like($someOtherUserId);
Like::like($post, $someOtherUserId);
Authentication requirement

If you don't pass a user id and Auth::id() returns null or 0, an Illuminate\Auth\AuthenticationException is thrown. Make sure the user is logged in, or pass an explicit user id.

What happens under the hood​

All three action methods are idempotent and single-active:

  • Idempotent: calling like() on a model you already liked returns the existing Like row without creating a duplicate.
  • Single-active: a user can only have one interaction type per model at a time. Setting a new type deletes any other existing interaction for that user + model combination, all inside a database transaction.
$post->like(); // Creates a LIKE row
$post->like(); // Returns existing LIKE row — no duplicate
$post->dislike(); // Deletes the LIKE row, creates a DISLIKE row

The returned Like model gives you full access to the interaction record:

$like = $post->like();

$like->type; // InteractionTypeEnum::LIKE
$like->user_id; // 1
$like->model_id; // 1
$like->model_type; // 'App\Models\Post'
$like->created_at; // Carbon instance

Model must be saved first​

The model must already exist in the database (i.e. $model->exists must be true). Interacting with an unsaved model throws an InvalidArgumentException:

$post = new Post(['title' => 'New']);
// ❌ Throws InvalidArgumentException — model is not persisted
$post->like();

$post->save(); // First save the model
$post->like(); // ✅ Works

Transactional safety​

All actions are wrapped in database transactions. If anything fails (database constraint, race condition), the transaction is rolled back automatically — no partial rows are created.

Next Steps​