🔧 Installation
Please read carefully the instructions below and follow them step by step.
1. Install the package
You can install the package via Composer:
composer require cslant/laravel-like
The package will register its CSlant\LaravelLike\Providers\LikeServiceProvider automatically via package auto-discovery.
2. Publish the configuration and migration files
This is a required step. Please don't skip it.
php artisan vendor:publish --provider="CSlant\LaravelLike\Providers\LikeServiceProvider"
This publishes two things:
config/like.php— the package configuration filedatabase/migrations/— the migrations for the interactions table:<timestamp>_create_likes_table.php— creates thelikestable<timestamp>_add_type_lookup_index_to_likes_table.php— adds a composite index (model_type,model_id,type) that speeds uplikesCount(),dislikesCount(),lovesCount(), andlikeCountsFor()
You can also publish them separately:
php artisan vendor:publish --provider="CSlant\LaravelLike\Providers\LikeServiceProvider" --tag=config
php artisan vendor:publish --provider="CSlant\LaravelLike\Providers\LikeServiceProvider" --tag=migrations
See the configuration page for every available option (is_uuids, table_name, interaction_model, users.model, users.foreign_key).
3. Run the migration
After the configuration file has been published, run the migration:
php artisan migrate
The migration creates a single likes table that stores all interactions (likes, dislikes, loves) for all interactable models through a polymorphic model column pair.
Table structure:
id,model_id,model_type(polymorphic morphs),user_id,type,created_at,updated_at, with a unique constraint on(user_id, model_id, model_type, type)and a composite index on(model_type, model_id, type)for fast per-model count lookups.
4. Add the trait to your content model
Add the HasLike trait to any model you want to make interactable. This provides like and dislike functionality.
namespace App\Models;
use CSlant\LaravelLike\HasLike;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasLike;
// Your model code here
}
If you only need the love ❤️ surface, use HasLove instead (or in addition):
use CSlant\LaravelLike\HasLove;
class Post extends Model
{
use HasLike;
use HasLove;
// Now supports: like, dislike AND love
}
UserHasInteraction is meant for the User model. It defines a likes() hasMany relationship, which collides with the likes() morphMany relationship defined by HasLike/HasLove. Never compose UserHasInteraction together with HasLike or HasLove on the same model.
5. Add the trait to your User model
To let the authenticated user interact (and to fetch their interactions), add the UserHasInteraction trait to your User model:
namespace App\Models;
use CSlant\LaravelLike\UserHasInteraction;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use UserHasInteraction;
// Your model code here
}
UserHasInteraction adds:
likes()— aHasManyrelationship with all the user's interactions (likes, dislikes, loves)forgetInteractions()/forgetInteractionsOfType()— bulk-delete the user's interactions
6. Optional: register the facade
The Like and Love facades are usable out of the box via their full class name:
use CSlant\LaravelLike\Facades\Like;
Like::like($post);
Everything works without registering anything extra. If you want shorter aliases, add them to the aliases array in config/app.php:
'aliases' => [
// ...
'Like' => \CSlant\LaravelLike\Facades\Like::class,
],
Done 🎉
That's it! You have successfully installed the Laravel Like package. Now head over to:
- Liking content to create your first interaction
- The LikeManager & Facade API for the full API reference
- Performance to keep your queries fast