Skip to content

spatie/laravel-relatable

Repository files navigation

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

Latest Version on Packagist Software License Build Status SensioLabsInsight Quality Score Total Downloads

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.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

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. We publish all received postcards on our virtual postcard wall.

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

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

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

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

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

Please see CHANGELOG for more information what has changed recently.

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.

Credits

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.

License

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