Skip to main content
Back to packages
757 GitHub stars

maize-tech/laravel-markable

View on GitHub

Description

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

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
 
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,
];
}

You can now assign likes to the model:

use App\Models\Course;
use Maize\Markable\Models\Like;
 
$course = Course::firstOrFail();
$user = auth()->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

Related Content on Laravel Daily

Video

Recent Courses on Laravel Daily

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.