Blog Detail

12

Apr
Add the Markable Feature to Your App with Laravel Markable cover image

arrow_back Add the Markable Feature to Your App with Laravel Markable

H-FARM Innovaationintroduced an amazing package called Laravel Markable that allows you to easily add the markable feature to your application, for example, likes, bookmarks, favorites, reactions, custom marks, and so on.

Installation

You can install the package via composer:

composer require maize-tech/laravel-markable

You can publish and run the migrations with:

php artisan vendor:publish --tag="markable-migration-bookmark" # publishes bookmark migration
php artisan vendor:publish --tag="markable-migration-favorite" # publishes favorite migration
php artisan vendor:publish --tag="markable-migration-like"  # publishes like migration
php artisan vendor:publish --tag="markable-migration-reaction"  # publishes reaction migration

php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="markable-config"

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 by 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

Custom mark model

The package allows you to define custom marks.

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

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

Here’s an example migration for bookmarks:

class CreateBookmarksTable 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 that extends the abstract Mark class and implement the markableRelationName method, which defines the name of the relation.

Here’s an example model for bookmarks:

<?php

namespace App\Models;

use Maize\Markable\Mark;

class Bookmark extends Mark
{
    public static function markableRelationName(): string
    {
        return 'bookmarkers';
    }
}

That’s all! You can now include the custom mark to all models you wish and use it as explained before.

For more details, you can visit its complete documentation and source code on Github.

Published at : 12-04-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project