PHPackages                             dandelionmood/laravel-relatable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. dandelionmood/laravel-relatable

AbandonedArchivedLibrary[Database &amp; ORM](/categories/database)

dandelionmood/laravel-relatable
===============================

Trait to manage an Eloquent model's related content

1.0.0(10y ago)09MITPHPPHP ^7.0

Since Mar 2Pushed 7y ago1 watchersCompare

[ Source](https://github.com/dandelionmood/laravel-relatable)[ Packagist](https://packagist.org/packages/dandelionmood/laravel-relatable)[ Docs](https://github.com/spatie/laravel-relatable)[ RSS](/packages/dandelionmood-laravel-relatable/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (4)Versions (7)Used By (0)

**NOTE: This package is not registered on Packagist and is abandoned. It's only being use in a few internal projects at Spatie**

Trait to Manage an Eloquent Model's Related Content
===================================================

[](#trait-to-manage-an-eloquent-models-related-content)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d555691c25249a55f7fbbe7dcb3f6d39280c7f21856baa436f0094b6577c1f7f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f6c61726176656c2d72656c617461626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-relatable)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/62afb7143a4336276c987ce6746d97862a948eccc8d43f2df607ac5c6d7febf4/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7370617469652f6c61726176656c2d72656c617461626c652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/spatie/laravel-relatable)[![SensioLabsInsight](https://camo.githubusercontent.com/4f53b6dcfb7c41afddd5b2bbe4e49d0b9b2bb94a221b718b1dc51afeecb37fc4/68747470733a2f2f696d672e736869656c64732e696f2f73656e73696f6c6162732f692f7878787878787878782e7376673f7374796c653d666c61742d737175617265)](https://insight.sensiolabs.com/projects/xxxxxxxxx)[![Quality Score](https://camo.githubusercontent.com/88dce45756f1d2eb892a8974ee952893bd0853995e2cb7ba98645060fff94532/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7370617469652f6c61726176656c2d72656c617461626c652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/spatie/laravel-relatable)[![Total Downloads](https://camo.githubusercontent.com/a5a5a5213bb969738b4c813e2311d7591d5ebd742c7ec6e975400620c5178449/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6c61726176656c2d72656c617461626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-relatable)

The `laravel-relatable` package provides a `HasRelatedContent` trait, which allows you to easily relate models to other models of any type.

```
// The `Post` class uses the `HasRelatedContent` trait
$post = Post::find(1);

$anotherPost = Post::find(2);
$person = Person::find(1);

$post->relate($anotherPost);
$post->relate($person);
```

Afterwards, you can retrieve the post's related content via the `related` accessor.

```
$related = $post->related;
// => Collection containing `$anotherPost` and `$person`
```

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

Install
-------

[](#install)

You can install the package via composer:

```
composer require spatie/laravel-relatable
```

In order to publish the migrations and configuration file, you'll need to register the service provider:

```
// config/app.php
'providers' => [
    // ...
    Spatie\Relatable\RelatableServiceProvider::class,
];
```

If you want to specify a custom table name, you'll need to publish and edit the configuration file:

```
php artisan vendor:publish --provider="Spatie\Relatable\RelatableServiceProvider" --tag="config"
```

Publishing and running the migrations is mandatory:

```
php artisan vendor:publish --provider="Spatie\Relatable\RelatableServiceProvider" --tag="migrations"
php artisan migrate
```

Usage
-----

[](#usage)

After running the migrations, you can start using the package by adding the `HasRelatedContent` trait to your models.

```
use Illuminate\Database\Eloquent\Model;
use Spatie\Relatable\HasRelatedContent;

class Post extends Model
{
    use HasRelatedContent;
}
```

### Adding and Removing Related Content

[](#adding-and-removing-related-content)

You can add related content to a model using the `relate` function. `relate` expects a model or an ID and type as parameters.

```
$post->relate($anotherPost);
$post->relate($anotherPost->id, Post::class);
```

Removing related content happens with the `unrelate` function, which expects the same parameters.

```
$post->unrelate($anotherPost);
$post->unrelate($anotherPost->id, Post::class);
```

### Synchronizing Related Content

[](#synchronizing-related-content)

Related content can be synced like Laravel's sync function for many-to-many relationships. The first parameter of `syncRelated` should be a collection of Eloquent models or an array containing associated arrays with ID's and types.

```
// Relate all magic posts
$post->syncRelated(Post::where('magic', true)->get());

// Relate post #1
$post->syncRelated([['id' => 1, 'type' => Post::class]]);
```

By default, `syncRelated` will detach all other related models. If you just want to add related content, set the `detach` parameter to false.

```
// Relate all magic posts, without detaching other related content
$post->syncRelated(Post::where('magic', true)->get());
```

### Retrieving Related Content

[](#retrieving-related-content)

The `HasRelatetContent` trait provides an accessor for `related`. Related content will be loaded and cached in memory the first time this function is called.

```
$post->related; // : \Illuminate\Support\Collection
```

The related content can be manually reloaded via the `loadRelated` method. This method will refill the related cache, and return the collection.

```
$post->loadRelated(); // : \Illuminate\Support\Collection
```

A `hasRelated` helper function is also provided.

```
$post->hasRelated(); // : bool
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

```
composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security
--------

[](#security)

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

Credits
-------

[](#credits)

- [Sebastian De Deyne](https://github.com/sebastiandedeyne)
- [All Contributors](../../contributors)

About Spatie
------------

[](#about-spatie)

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 81.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

6

Last Release

3716d ago

Major Versions

0.2.1 → 1.0.02016-03-08

### Community

Maintainers

![](https://www.gravatar.com/avatar/e0226d48672e9b63997b511cf7448ce9cf670ecbc6499f9c602d99d70f8de6ea?d=identicon)[dandelionmood](/maintainers/dandelionmood)

---

Top Contributors

[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (18 commits)")[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (2 commits)")[![dandelionmood](https://avatars.githubusercontent.com/u/182812?v=4)](https://github.com/dandelionmood "dandelionmood (1 commits)")[![Tjoosten](https://avatars.githubusercontent.com/u/5157609?v=4)](https://github.com/Tjoosten "Tjoosten (1 commits)")

---

Tags

spatielaravel-relatable

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/dandelionmood-laravel-relatable/health.svg)

```
[![Health](https://phpackages.com/badges/dandelionmood-laravel-relatable/health.svg)](https://phpackages.com/packages/dandelionmood-laravel-relatable)
```

###  Alternatives

[spatie/laravel-sluggable

Generate slugs when saving Eloquent models

1.6k11.5M223](/packages/spatie-laravel-sluggable)[spatie/laravel-medialibrary

Associate files with Eloquent models

6.1k37.7M472](/packages/spatie-laravel-medialibrary)[spatie/laravel-backup

A Laravel package to backup your application

6.0k21.8M190](/packages/spatie-laravel-backup)[spatie/laravel-translatable

A trait to make an Eloquent model hold translations

2.4k23.0M413](/packages/spatie-laravel-translatable)[spatie/laravel-schemaless-attributes

Add schemaless attributes to Eloquent models

1.1k8.7M62](/packages/spatie-laravel-schemaless-attributes)[spatie/laravel-model-states

State support for Eloquent models

1.3k6.2M27](/packages/spatie-laravel-model-states)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
