Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

laravel-markable

> 前端框架
Open source

Integrate likes, bookmarks, favorites, reactions and custom made marks into your application

759 stars0 likes3 views
WebsiteGitHub

About

Integrate likes, bookmarks, favorites, reactions and custom made marks into your application

Laravel Markable

This package allows you to easily add the markable feature to your application, as for example likes, bookmarks, favorites and so on.

Installation

You can install the package via composer:

bash
composer require maize-tech/laravel-markable
bash
php artisan markable:install

The install command will automatically publish the config file. Then publish only the migrations you need:

bash
php artisan vendor:publish --tag="markable-migration-bookmark"
bash
php artisan vendor:publish --tag="markable-migration-favorite"
bash
php artisan vendor:publish --tag="markable-migration-like"
bash
php artisan vendor:publish --tag="markable-migration-reaction"
bash
php artisan migrate

This is the content of the published config file:

…

Usage

Basic

To use the package, add the Maize\Markable\Markable trait to the model where you want to have marks.

Once done, you can define the list of possible marks for the given model implementing the $marks array with the list of mark classes' namespace.

Here's an example model including the Markable trait and implementing the Like mark:

php
user();

Like::add($course, $user); // marks the course liked for the given user

Like::remove($course, $user); // unmarks the course liked for the given user

Like::toggle($course, $user); // toggles the course like for the given user

Like::has($course, $user); // returns whether the given user has marked as liked the course or not

Like::count($course); // returns the amount of like marks for the given course

Custom metadata

If needed, you may also add custom metadata when assigning a mark:

php
use App\Models\Course;
use Maize\Markable\Models\Like;

$course = Course::firstOrFail();
$user = auth()->user();

Like::add($course, $user, [
    'topic' => $course->topic,
]);

Like::toggle($course, $user, [
    'topic' => $course->topic,
]);

Custom mark model

The package allows you to define custom marks.

First thing you need to do is create a migration which defines the new mark model. The package works with separate tables for each mark in order to increase the performances when executing related queries.

The migration table name should contain the prefix defined in table_prefix attribute under config/markable.php. Default prefix is set to markable_.

Here's an example migration for bookmarks:

php
return new class extends Migration
{
    public function up()
    {
        Schema::create('markable_bookmarks', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnUpdate()->cascadeOnDelete();
            $table->morphs('markable');
            $table->string('value')->nullable();
            $table->json('metadata')->nullable();
            $table->timestamps();
        });
    }
}

Once done, you can create a new class which extends the abstract Mark class and implement the markableRelationName method, which is used to retrieve the users who marked a given model entity with the mark entity as pivot.

You can also override the markRelationName method, which is used to retrieve the list of marks of a given model entity. By default, the relation name is the plural name of the mark class name.

Here's an example model for the bookmarks mark:

php
 [
    'reaction' => [
        'person_raising_hand',
        'heart',
        'kissing_heart',
    ],
],

You can then use the custom mark with values:

…

You can also use wildcards to allow any value for a specific mark.

Here's an example when working with reactions:

php
'allowed_values' => [
    'reaction' => '*',
],

When set, you can use any value when giving a reaction:

php
use App\Models\Post;
use Maize\Markable\Models\Reaction;

$post = Post::firstOrFail();
$user = auth()->user();

Reaction::add($post, $user, 'random_value'); // adds the 'random_value' reaction to the post for the given user

Using BackedEnum values

You can use a BackedEnum class as the allowed_values for a mark. This lets you leverage PHP enums for type-safe mark values instead of plain strings.

Define your enum:

php
// app/Enums/ReactionType.php
enum ReactionType: string
{
    case Like = 'like';
    case Love = 'love';
    case Wow = 'wow';
}

Set the enum class as the allowed values in config/markable.php:

php
'allowed_values' => [
    'reaction' => \App\Enums\ReactionType::class,
],

All mark methods accept null|string|\BackedEnum, so you can pass enum cases directly:

php
use App\Enums\ReactionType;
use App\Models\Post;
use Maize\Markable\Models\Reaction;

$post = Post::firstOrFail();
$user = auth()->user();

Reaction::add($post, $user, ReactionType::Like);
Reaction::remove($post, $user, ReactionType::Like);
Reaction::toggle($post, $user, ReactionType::Love);
Reaction::has($post, $user, ReactionType::Like); // bool
Reaction::count($post, ReactionType::Wow); // int

The dynamic Eloquent scopes also accept enum cases:

php
Post::whereHasReaction($user, ReactionType::Like)->get();

Retrieve the list of marks of an entity with eloquent

php
use App\Models\Course;
use App\Models\Post;

Course::firstOrFail()->likes; // returns the collection of like marks related to the course
Post::firstOrFail()->reactions; // returns the collection of reaction marks related to the post 

Retrieve the list of users who marked an entity with eloquent

php
use App\Models\Course;
use App\Models\Post;

Course::firstOrFail()->likers; // returns the collection of users who liked the course along with the mark value as pivot
Post::firstOrFail()->reacters; // returns the collection of users who reacted to the post along with the mark value as pivot

Filter marked models with eloquent

php
use App\Models\Course;
use App\Models\Post;

Course::whereHasLike(
    auth()->user()
)->get(); // returns all course models with a like from the given user

Post::whereHasReaction(
    auth()->user(),
    'heart'
)->get(); // returns all post models with a 'heart' reaction from the given user

Testing

bash
composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

  • Enrico De Lazzari
  • Riccardo Dalla Via
  • All Contributors

License

The MIT License (MIT). Please see License File for more information.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPbookmarksfavoriteslaravellikes

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架