Skip to main content

🛠 Configuration

Here is the default configuration for Laravel Like package. You can customize the configuration as per your requirements.

Configuration file

Path: config/like.php

config/like.php
return [
/*
* The flag to determine if the interactions table should use UUIDs.
* If you want to use UUIDs instead of auto-incrementing integers
* for your interactions table, set this to true.
*/
'is_uuids' => false,

/*
* The table name for interaction records.
*/
'table_name' => 'likes',

/*
* The model class for the interaction table.
*/
'interaction_model' => 'CSlant\LaravelLike\Models\Like',

/*
* The model and foreign key for the user relationship.
*/
'users' => [
/*
* User model class.
* When null, the package falls back to
* config('auth.providers.users.model') automatically.
*/
'model' => null,

/*
* User tables foreign key name.
* Use this to set the foreign key name for the user relationship.
*/
'foreign_key' => 'user_id',
],

/*
* Caching for per-type interaction counts (likesCount, dislikesCount, lovesCount).
* Counts are read far more often than they change, so caching them cuts repeated
* COUNT queries on hot paths (feeds, listings). Disabled by default to keep the
* package's out-of-the-box behaviour always consistent; enable it once your cache
* store is configured.
*/
'cache' => [
'enabled' => false,

/*
* Time-to-live in seconds for a cached count. The cache is also actively
* invalidated whenever an interaction is created, moved, or removed, so this
* TTL is only a safety net.
*/
'ttl' => 60,
],
];
Note

If you want to change the configuration, you can publish the configuration file in the installation step.

And if you have already run the migration, you need to roll back the migration and run it again to apply the changes.

php artisan migrate:rollback

If you have rolled back the migration, please follow the modification instructions below to customize it to suit your project.


Configuration options

is_uuids

Use UUIDs instead of auto-incrementing integers for the id column in the likes table.

'is_uuids' => true,

What happens when enabled:

  • The migration uses uuid('id')->primary() instead of id()
  • Morph columns use uuid instead of the default integer
  • The CSlant\LaravelLike\Models\Like model automatically generates UUIDs on insert
  • No additional model changes are required — the behaviour is config-driven

When to use it: choose is_uuids if you have non-sequential IDs in your application or are running on a database (like CouchDB or MongoDB via a driver) that doesn't support auto-incrementing integers.

Important

After changing is_uuids, you must rollback and re-run your migrations. You also need to migrate any existing data manually — the package does not handle this for you.


table_name

Customise the interactions table name:

'table_name' => 'interactions',

Make sure to re-run the migration after changing this value.


interaction_model

Specify a custom interaction model to replace the default CSlant\LaravelLike\Models\Like class. Your custom model must extend the default one:

'interaction_model' => \App\Models\CustomLike::class,
namespace App\Models;

use CSlant\LaravelLike\Models\Like;

class CustomLike extends Like
{
// Add custom relationships, attributes, or casts
}

This is useful if you want to add extra columns (via a separate migration), custom relationships, or additional logic to each interaction record.


users.model

Set the user model class. When null (the default), the package automatically resolves the model from config('auth.providers.users.model') — the standard Laravel auth user model.

'users' => [
'model' => null, // Falls back to auth()->user() model
],

Change this only if your User model lives in a different namespace:

'users' => [
'model' => \App\Modules\User\CustomUser::class,
],

users.foreign_key

If your likes table uses a different column name for the user foreign key, update this:

'users' => [
'foreign_key' => 'author_id',
],

Make sure the migration table uses the same column name.


cache.enabled

Turns on caching for likesCount(), dislikesCount(), and lovesCount() (both the model helpers and the LikeManager/facade methods):

'cache' => [
'enabled' => true,
],

What happens when enabled:

  • Each per-type count is cached under a key like like:count:like:App\Models\Post:42.
  • The cache is automatically invalidated whenever the underlying interaction changes — like(), dislike(), love(), unlike(), unDislike(), unlove(), and toggle() all bust the cache for that model.
  • totalCount() is not cached — only the three per-type counts.

Uses your application's default cache store (config('cache.default')). No extra setup needed beyond having a cache driver configured.

When to enable it

Leave this false unless you have a specific hot page (a viral post, a trending feed) where the same count is read many times per second. For most applications, an uncached COUNT query on an indexed column is already fast enough — see Performance.


cache.ttl

Time-to-live, in seconds, for a cached count:

'cache' => [
'ttl' => 300, // 5 minutes
],

Since the cache is actively invalidated on every write, the TTL is only a safety net (e.g. for writes made outside the package, directly on the likes table). Defaults to 60.


Re-run the migration

After you have made any changes to the configuration file, you need to re-run the migration to apply them:

php artisan migrate:rollback
php artisan migrate

That's it! You have successfully configured the Laravel Like package.