• Reading time ~ 2 min
  • 20.06.2022

Laravel Markable is a package to Integrate likes, bookmarks, favorites, reactions, and custom-made marks into your application.

The basic usage is adding a Markable trait and types of "marks" you want to allow for a given model. For example, if you have a Course model and you want to let users like the course:

namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
use Maize\Markable\Markable;
use Maize\Markable\Models\Like;
 
class Course extends Model
{
    use Markable;
 
    protected $fillable = [
        'title',
        'description',
    ];
 
    protected static $marks = [
        Like::class,
    ];
}

With that model in place, here's what it looks like to manage marks using this package:

use App\Models\Course;
use Maize\Markable\Models\Like;
 
// marks the course liked for the given user
Like::add($course, $user);
 
// unmarks the course liked for the given user
Like::remove($course, $user);
 
// toggles the course like for the given user
Like::toggle($course, $user);
 
// returns whether the given user likes a course or not
Like::has($course, $user);
 
// returns how many users like a course
Like::count($course);

The Like model is built-in to the package, but you can also build custom mark models, such as Pledge that allows a user to pledge a donation to a cause.

The package includes various mark types out-of-the-box, including:

  • Bookmark
  • Favorite
  • Like
  • Reaction

On the other side of adding a Like for a user, you might what to list all likes for a given user so they can see what courses they've liked:

// All course models with a like from the given user
Course::whereHasLike(
    auth()->user()
)->get();
 
// All post models with a 'heart' reaction from the given user
Post::whereHasReaction(
    auth()->user(),
    'heart'
)->get();

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

ABOUT

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

About author CrazyBoy49z
WORK EXPERIENCE
Contact
Ukraine, Lutsk
+380979856297