mmedia/laravel-collaborative-filtering

Simple trait to add collaborative filtering to your models

1.0.2 2022-12-05 13:13 UTC

This package is auto-updated.

Last update: 2024-12-05 17:22:38 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

Get related models for the current model. Commonly used for "similar products" sections.

Installation

You can install the package via composer:

composer require mmedia/laravel-collaborative-filtering

Usage

Imagine you have a model called Product, and each product has multiple ProductCategory records. You want to find products related to each other based on how many common categories they have (a.k.a using collaborative filtering). To do so, you can define a relationship in your Product model.

use MMedia\LaravelCollaborativeFiltering\HasCollaborativeFiltering;

class Product extends Model {

    use HasCollaborativeFiltering;

    public function related()
    {
        return $this->hasManyRelatedThrough(ProductCategory::class, 'category_id');
    }

    public function relatedThroughLikes()
    {
        return $this->hasManyRelatedThrough(ProductLikes::class, 'user_id');
    }

}

Based on the article from arctype.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email contact@mmediagroup.fr instead of using the issue tracker.

Credits

License

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

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.