Skip to content

Get related models for a given model - like when you want to show "related products" sections.

License

Notifications You must be signed in to change notification settings

M-Media-Group/Laravel-Collaborative-Filtering

Repository files navigation

Very short description of the package

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 [email protected] 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.

About

Get related models for a given model - like when you want to show "related products" sections.

Resources

License

Stars

Watchers

Forks

Packages

No packages published