PHPackages                             allcaretravel/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. allcaretravel/laravel-relatable

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

allcaretravel/laravel-relatable
===============================

Trait to manage an Eloquent model's related content

2.0.3(4y ago)024MITPHPPHP ^7.2.5|^8.0

Since Mar 2Pushed 4y agoCompare

[ Source](https://github.com/allcaretravel/laravel-relatable)[ Packagist](https://packagist.org/packages/allcaretravel/laravel-relatable)[ Docs](https://github.com/allcaretravel/laravel-relatable)[ RSS](/packages/allcaretravel-laravel-relatable/feed)WikiDiscussions master Synced 1mo ago

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

**NOTE: This project is forked from `spatie/laravel-relatable`**

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).

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/38a48edcc581aa4a2007f63b501d5b6d675efe033da49a29dd9fea464ab57aa7/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6c61726176656c2d72656c617461626c652e6a70673f743d31)](https://spatie.be/github-ad-click/laravel-relatable)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Install
-------

[](#install)

You can install the package via composer:

```
composer require allcaretravel/laravel-relatable
```

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

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

Publishing and running the migrations is mandatory:

```
php artisan vendor:publish --provider="Act\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 Act\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)
- [Sophy SEM](https://github.com/semsphy)
- [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

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 62.1% 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 ~214 days

Recently: every ~482 days

Total

10

Last Release

1790d ago

Major Versions

0.2.1 → 1.0.02016-03-08

1.0.0 → 2.0.02021-06-17

PHP version history (2 changes)0.1.0PHP ^7.0

2.0.0PHP ^7.2.5|^8.0

### Community

Maintainers

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

---

Top Contributors

[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (18 commits)")[![semsphy](https://avatars.githubusercontent.com/u/423396?v=4)](https://github.com/semsphy "semsphy (5 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (3 commits)")[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (2 commits)")[![Tjoosten](https://avatars.githubusercontent.com/u/5157609?v=4)](https://github.com/Tjoosten "Tjoosten (1 commits)")

---

Tags

spatielaravel-relatableallcaretravel

###  Code Quality

TestsPHPUnit

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/allcaretravel-laravel-relatable/health.svg)](https://phpackages.com/packages/allcaretravel-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.8M191](/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)
