PHPackages                             eroslover/laravel-references - 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. eroslover/laravel-references

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

eroslover/laravel-references
============================

References between models in Laravel

1.0.5(6y ago)187MITPHPPHP &gt;=7.0.0

Since Jun 30Pushed 6y agoCompare

[ Source](https://github.com/eroslover/laravel-references)[ Packagist](https://packagist.org/packages/eroslover/laravel-references)[ Docs](https://github.com/eroslover/laravel-references)[ RSS](/packages/eroslover-laravel-references/feed)WikiDiscussions master Synced 3d ago

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

Laravel Model References
========================

[](#laravel-model-references)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

This Laravel package provides a quick and simple way to make references between any Eloquent models.

Here are a few short examples of what you can do:

```
// photo of some persons
$photo = Photo::find(1);

// persons you want to refer with this photo
$person1 = Person::find(1);
$person2 = Person::find(2);

// making a reference
$photo->ref($person1);
$photo->ref($person2);

// you are able to refer a collection of persons
$persons = Person::find([1, 2]);
$photo->ref($persons);
```

Installation
------------

[](#installation)

You can install the package via composer:

```
$ composer require eroslover/laravel-references
```

The service provider will automatically get registered. Or you may manually add the service provider in your config/app.php file:

```
'providers' => [
    ...
    Eroslover\References\ReferencesServiceProvider::class,
];
```

Now publish the migration and config with:

```
php artisan vendor:publish --provider="Eroslover\References\ReferencesServiceProvider"
```

This is the contents of the published config file:

```
return [
    /*
     * Name of the database table that will store model references.
     */
    'table_name' => 'references'
];
```

Here you can just change the name of the table that will be used for references.

After the migration has been published you can create the references-table by running the migrations:

```
php artisan migrate
```

Usage
-----

[](#usage)

Choose the model you want to add references to. As in example above, I'll choose `Photo`. This class should implement `ReferencesInterface` and import `References` trait.

```
namespace App;

use Eroslover\References\Traits\References;
use Eroslover\References\Interfaces\ReferenceInterface;
use Illuminate\Database\Eloquent\Model;

class Photo extends Model implements ReferenceInterface
{
    use References;
}
```

Choose the models you want to refer to the photo. For example `Person`, `Location`and `Event`.

```
namespace App;

use Illuminate\Database\Eloquent\Model;

class Person extends Model {}
class Location extends Model {}
class Event extends Model {}
```

##### Making references

[](#making-references)

The `ref` method accepts `Model` or `Collection` of models to put data in a references table:

```
$photo = Photo::find(1);

$location = Location::find(3);
$persons = Person::whereLocation($location->id)->get();
$event = Event::first();

$photo->ref($location);
$photo->ref($persons);
$photo->ref($event);
```

##### Removing references

[](#removing-references)

The `unref` method accepts `Model` or `Collection` of models to remove them from the references table:

```
$photo->unref($location);
```

##### Syncing references

[](#syncing-references)

The `syncRefs` method accepts `null`, `Model` or `Collection` of models to put data or remove data from the references table. Any models that are not in the given collection will be removed from the references table. So, when this operation is complete, only models in the given collection will exist in the reference table for chosen model:

```
$photo->syncRefs($referencable);
```

##### Retrieving references

[](#retrieving-references)

The `loadReferences` method returns the collection of referenced models. Accepts boolean `$grouped` parameter. By default, method returns mapped collection where the key is namespace and value is a collection of entities. If you need to get a collection of referenced entities only, you'll need to pass `false` to method as an argument:

```
$photo->loadReferences();

Output:

ReferenceCollection {#1715 ▼
  #items: array:3 [▼
    "App\Modules\Location" => Collection {#1730 ▼
      #items: array:1 [▼
        0 => Location {#1731 ▶}
      ]
    }
    "App\Modules\Person" => Collection {#1733 ▼
      #items: array:2 [▼
        0 => Person {#1734 ▶}
        1 => Person {#1735 ▶}
      ]
    }
    "App\Modules\Event" => Collection {#1737 ▼
      #items: array:1 [▼
        0 => Event {#1738 ▶}
      ]
    }
  ]
}
```

```
$photo->loadReferences(false);

Output:

Collection {#1716 ▼
  #items: array:4 [▼
    0 => Location {#1731 ▶}
    1 => Person {#1732 ▶}
    2 => Person {#1734 ▶}
    3 => Event {#1735 ▶}
  ]
}
```

Testing
-------

[](#testing)

You can run tests with:

```
$ vendor/bin/phpunit
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](https://github.com/eroslover/laravel-references/blob/master/LICENSE.md) for more information.

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

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 ~2 days

Total

6

Last Release

2549d ago

PHP version history (2 changes)1.0.0PHP &gt;=7.0

1.0.1PHP &gt;=7.0.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15260436?v=4)[Yaroslav Shikovets](/maintainers/eroslover)[@eroslover](https://github.com/eroslover)

---

Tags

laravelmodelsreferenceslaravel-referenceseroslover

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/eroslover-laravel-references/health.svg)

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

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3345.1M337](/packages/psalm-plugin-laravel)[larastan/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

6.4k51.0M7.8k](/packages/larastan-larastan)[laravel/mcp

Rapidly build MCP servers for your Laravel applications.

76922.3M129](/packages/laravel-mcp)[api-platform/laravel

API Platform support for Laravel

59156.3k11](/packages/api-platform-laravel)[simplestats-io/laravel-client

Analytics for Laravel. Track visitors, registrations, and payments. Discover which channels actually drive revenue, not just traffic. Server-side, GDPR compliant, ad-blocker proof.

5019.3k](/packages/simplestats-io-laravel-client)[calebdw/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

15104.9k4](/packages/calebdw-larastan)

PHPackages © 2026

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