PHPackages                             masterfri/laravel-smart-relations - 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. masterfri/laravel-smart-relations

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

masterfri/laravel-smart-relations
=================================

Work with relations as simple as with attributes.

v1.2(7y ago)71.6k1MITPHPPHP &gt;=7.1.0CI failing

Since Nov 18Pushed 5y ago2 watchersCompare

[ Source](https://github.com/masterfri/laravel-smart-relations)[ Packagist](https://packagist.org/packages/masterfri/laravel-smart-relations)[ RSS](/packages/masterfri-laravel-smart-relations/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (4)Versions (4)Used By (0)

Laravel Smart Relations
=======================

[](#laravel-smart-relations)

This library allows to work with relations in the way as if they were attributes. To make a relationship, you only need to assign a value (or values in case of mass assignment) to model and create/save it.

For now the following relation types are supported: BelongsTo, BelongsToMany, HasOne, HasMany, MorphTo, MorphToMany, MorphOne, MorphMany.

Installation
============

[](#installation)

`composer require masterfri/laravel-smart-relations`

Examples
========

[](#examples)

```
use Masterfri\SmartRelations\SmartRelations;

class Library extends Model
{
    // To enable smart relations add the following trait in your model
    use SmartRelations;

    // Relations can be listed in $fillable property to enable mass assignment
    protected $fillable = ['name', 'books', 'readers'];

    // If you want children records to be deleted along with parent record
    // you can list required relation names in $cascade_delete property.
    // For example, when library instance is deleted all related books
    // will be deleted as well, and readers will be detached (not deleted, since
    // it is BelongsToMany relationship)
    protected $cascade_delete = ['books', 'readers'];

    public function books()
    {
        return $this->hasMany(Book::class);
    }

    public function readers()
    {
        return $this->belongsToMany(Reader::class, 'subscriptions');
    }
}

class Book extends Model
{
    use SmartRelations;

    protected $fillable = ['title', 'library'];

    public function library()
    {
        return $this->belongsTo(Library::class);
    }

    public function card()
    {
        return $this->hasOne(BookCard::class);
    }
}

class Reader extends Model
{
    use SmartRelations;

    protected $fillable = ['name'];
}

class BookCard extends Model
{
    use SmartRelations;

    protected $fillable = ['book', 'reader'];

    public function book()
    {
        return $this->belongsTo(Book::class);
    }

    public function reader()
    {
        return $this->belongsTo(Reader::class);
    }
}

// HasMany relation example
// =========================
$library = Library::create([
    'name' => 'Central Library',
    // Related records can be passed as array or model instance
    // If instances does not exist in database, they will be created
    // You also can pass ID of existing record to attach it
    'books' => [
        ['title' => 'First Book'],
        new Book(['title' => 'Second Book']),
        $existingBook,
        123,
    ],
]);
// or
$library->books = [
    ['title' => 'First Book'],
    new Book(['title' => 'Second Book']),
    $existingBook,
    123,
];
$library->save();

// HasOne relation example
// =========================
$book = Book::create([
    'title' => 'Third book',
    'card' => new BookCard(),
    // or simply
    // 'card' => [],
]);

// BelongsTo relation example
// =========================
$book = Book::create([
    'title' => 'Third book',
    // In that way model can be associated with parent record
    'library' => $library,
]);
// or
$book->library = $library;
$book->save();

// BelongsToMany relation example
// =========================
$reader1 = Reader::create(['name' => 'John']);
$reader2 = Reader::create(['name' => 'Jack']);

// In case of many to many relation you can pass model instance/ID to make relationship
$library->readers = [$reader1, $reader2->id];
$library->save();

// Update data on related records
// =========================
// If you pass primary key in the data, related records will be loaded
// from database, and their attributes will be updated. But bear in mind
// that related records which are not listed in the array will be deleted,
// because all children records will be replaced with new data.
$library->books = [
    ['id' => 1, 'title' => 'First Book New Name'],
    ['id' => 2, 'title' => 'Second Book New Name'],
];
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~50 days

Total

3

Last Release

2629d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1f854cc94473cbef444a834643dc9975b1ba2014fb8a3eef16e21363557e0175?d=identicon)[masterfri](/maintainers/masterfri)

---

Top Contributors

[![masterfri](https://avatars.githubusercontent.com/u/299635?v=4)](https://github.com/masterfri "masterfri (12 commits)")

---

Tags

eloquentlaravelrelationshipslaraveleloquentrelations

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/masterfri-laravel-smart-relations/health.svg)

```
[![Health](https://phpackages.com/badges/masterfri-laravel-smart-relations/health.svg)](https://phpackages.com/packages/masterfri-laravel-smart-relations)
```

###  Alternatives

[korridor/laravel-has-many-merged

Custom relationship for Eloquent that merges/combines multiple one-to-may (hasMany) relationships

90122.5k1](/packages/korridor-laravel-has-many-merged)[korridor/laravel-has-many-sync

Laravel has many sync

3578.1k](/packages/korridor-laravel-has-many-sync)

PHPackages © 2026

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