allcaretravel/laravel-relatable

Trait to manage an Eloquent model's related content

2.0.3 2021-06-17 10:41 UTC

This package is auto-updated.

Last update: 2024-04-17 16:39:56 UTC


README

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

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

68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6c61726176656c2d72656c617461626c652e6a70673f743d31

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

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

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 discover any security related issues, please email semsphy@gmail.com 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.