---
title: "Models & traits — Likes for Laravel | Roundly"
description: "Add HasLikes to models that can be liked and GivesLikes to actors; the relations, the Likeable contract and the Like model."
url: https://roundly-consulting.com/open-source/docs/likes-for-laravel/models
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Likes for Laravel](https://roundly-consulting.com/open-source/docs/likes-for-laravel.md)

# Models & traits

Add the HasLikes trait to models that can be liked and the GivesLikes trait to models that give likes. Any Eloquent model can play either role — users, teams, organisations:

```php
use Illuminate\Database\Eloquent\Model;
use RoundlyConsulting\Likes\Traits\GivesLikes;
use RoundlyConsulting\Likes\Traits\HasLikes;

class Post extends Model
{
    use HasLikes;     // can be liked
}

class User extends Model
{
    use GivesLikes;   // can give likes
}
```

## What each trait adds

- HasLikes — likes() and likesReceived() received, isLikedBy() / hasBeenLikedBy(), likesCount(), reactionSummary(), toLikeArray() and the query scopes (withLikesCount, orderByLikes, whereLikedBy, withLikedState, orderByLikeScore, orderByTrending …).
- GivesLikes — likes() and likesGiven() given, like(), unlike(), toggleLike(), react() / switchReaction(), likeMany() / unlikeMany(), hasLiked(), likedItems() and likesOf().

The verbs and reads on both traits are sugar over the same manager as the Likes facade, so Likes::fake() records them too:

```php
$user->like($post);          // = Likes::actor($user)->like($post)
$post->likesCount();         // = Likes::for($post)->count()
$post->reactionSummary();    // = Likes::for($post)->summary()
```

## Relations

Both traits add a likes() MorphMany relation — the likes a model received on a likeable, the likes it gave on an actor:

```php
$post->likes;                                  // Collection<Like> — likes the post received
$user->likes;                                  // Collection<Like> — likes the user gave

$post->likes()->where('type', 'love')->count(); // a plain MorphMany — query it freely
$user->likes()->latest()->limit(5)->get();
```

## Models that give and receive likes

Both traits declare likes(), so a model using both — a member who likes posts and is liked by other members — resolves the collision with insteadof. Pick either side: the HasLikes scopes always count received likes, and likesReceived() / likesGiven() stay unambiguous:

```php
use Illuminate\Database\Eloquent\Model;
use RoundlyConsulting\Likes\Traits\GivesLikes;
use RoundlyConsulting\Likes\Traits\HasLikes;

class Member extends Model
{
    use GivesLikes, HasLikes {
        GivesLikes::likes insteadof HasLikes;
    }
}

$member->likesReceived()->count();   // likes on the member
$member->likesGiven()->count();      // likes the member gave
Member::query()->withLikesCount();   // received likes, whichever likes() won
```

## The Likeable contract

HasLikes implements every method of RoundlyConsulting\\Likes\\Contracts\\Likeable — isLikedBy(), likesCount(), reactionSummary() and toLikeArray(). Declare the interface when you want to type-hint against likeable models:

```php
use Illuminate\Database\Eloquent\Model;
use RoundlyConsulting\Likes\Contracts\Likeable;
use RoundlyConsulting\Likes\Traits\HasLikes;

class Comment extends Model implements Likeable
{
    use HasLikes;
}
```

## The Like model

Each row is a RoundlyConsulting\\Likes\\Models\\Like — a soft-deleting Eloquent model with actor() and likeable() morph-to relations and a factory (see Testing):

```php
$like = $post->likes()->latest()->first();

$like->actor;      // the model that gave the like (MorphTo)
$like->likeable;   // the model that received it (MorphTo)
$like->type;       // 'like', 'love', ...
$like->created_at; // when it was given
```

## Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

[More ways to support, including crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/likes-for-laravel.md)
