foxws/laravel-relatable

Relate Eloquent models to other models, with a base score and boost to control their priority.
1,087 1
Install
composer require foxws/laravel-relatable
Latest Version:v1.0.3
PHP:^8.3
Laravel:12 · 13
License:MIT
Last Updated:Oct 14, 2026
Links: GitHub  ·  Packagist
Maintainer: francoism90

Relate Eloquent models to other models — of any type — and control how strongly they relate. Each relation has a base score and a boost; their product, the relation's weight, orders the related models.

Relations are directed: Action can relate to Fastpace with a score of 1.0 and to Genre with 0.5, while Genre relates back to Action with a score of its own.

Installation

You can install the package via Composer:

composer require foxws/laravel-relatable

The migration for the relatables table runs automatically. You may publish all of the package's resources at once:

php artisan vendor:publish --tag="relatable"

Or, you may publish each resource individually:

php artisan vendor:publish --tag="relatable-config"
php artisan vendor:publish --tag="relatable-migrations"

[!TIP] Let your AI agent do the hard parts. This package ships a Laravel Boost skill, relatable-development, that teaches your agent how to install, configure and use Laravel Relatable. Install Boost and select the skill when prompted:

composer require laravel/boost --dev
php artisan boost:install

Already using Boost? Run php artisan boost:update --discover to pick up the skill. Then just ask, for example: "Relate my Video model to tags and order them by weight."

Usage

Add the InteractsWithRelated trait to each model that relates to others:

use Foxws\Relatable\Concerns\InteractsWithRelated;
use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
    use InteractsWithRelated;
}

Then relate models, with an optional score and boost:

$action->attachRelated($fastpace, score: 1.0);
$action->attachRelated($genre, score: 0.5);

// Relations are directed. Relate both ways (Action → Genre and Genre → Action),
// with a lower score back.
$action->attachRelated($genre, score: 0.5, mutual: true, mutualScore: 0.25);

// Relate exactly these models, removing the rest.
$action->syncRelated([
    $fastpace,
    ['model' => $genre, 'score' => 0.5, 'boost' => 2.0],
]);

$action->detachRelated($genre);               // only Action → Genre
$action->detachRelated($genre, mutual: true); // and Genre → Action

$action->relates; // Collection of related models, highest weight first
$action->getRelates(Video::class); // only related videos

See the documentation for mutual relations, scoring, querying relations, and configuration.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Thank you for considering contributing to Laravel Relatable! Please review our contributing guide to get started.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

Inspired by spatie/laravel-relatable.

License

Laravel Relatable is open-sourced software licensed under the MIT license.

Related Packages

korridor/laravel-has-many-merged

Custom relationship for Eloquent that merges/combines multiple one-to-may (hasMa...

155,125 90
czim/laravel-nestedupdater

Eloquent model updater for nested relations data for Laravel.

53,965 19
fish/eloquent-cascade

Cascading delete trait for Laravel's Eloquent

2,059 3

Version History

Version Released PHP Laravel License
v1.0.3 ^8.3 ^12.0|^13.0 MIT
v1.0.2 ^8.3 ^12.0|^13.0 MIT
v1.0.1 ^8.3 ^12.0|^13.0 MIT
v1.0.0 ^8.3 ^12.0|^13.0 MIT