PHPackages                             jarrodtomholt/belongs-to-macro - 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. jarrodtomholt/belongs-to-macro

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

jarrodtomholt/belongs-to-macro
==============================

Automatically add index and constraints to your migration foreignIdFor entities with a simple macro

1.0.0(3y ago)29MITPHPPHP ^8.0

Since Feb 14Pushed 3y ago1 watchersCompare

[ Source](https://github.com/jarrodtomholt/belongs-to-macro)[ Packagist](https://packagist.org/packages/jarrodtomholt/belongs-to-macro)[ Docs](https://github.com/jarrodtomholt/belongs-to-macro)[ RSS](/packages/jarrodtomholt-belongs-to-macro/feed)WikiDiscussions main Synced yesterday

READMEChangelogDependencies (3)Versions (2)Used By (0)

Laravel BelongsTo Macro
=======================

[](#laravel-belongsto-macro)

Automatically add index and foreign key constraints to your migrations enforcing data integrity, preserving the referential association and improving query performance.

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

[](#installation)

You can install the package via composer:

```
composer require jarrodtomholt/belongs-to-macro
```

Usage
-----

[](#usage)

In a migrations `up()` method, use the following in place of `foreignIdFor` to add indexes and foreign key constraints.

```
Schema::create('posts', function (Blueprint $table) {
    $table->belongsTo(User::class);
});

// will expand to
$table->foreignIdFor(User::class)->index()->constrained();
```

Need a nullable field? use `belongsToOrNull`

```
Schema::create('posts', function (Blueprint $table) {
    $table->belongsToOrNull(User::class);
});

// will expand to
$table->foreignIdFor(User::class)->nullable()->index()->constrained();
```

### Testing

[](#testing)

```
composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please raise an issue using the issue tracker.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

Laravel Package Boilerplate
---------------------------

[](#laravel-package-boilerplate)

This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

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

Unknown

Total

1

Last Release

1236d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/988b673681e7af553976bcfa11d66695b7e089a8d722ce07d29301c2b55d15ed?d=identicon)[jarrod.tomholt](/maintainers/jarrod.tomholt)

---

Top Contributors

[![jarrodtomholt](https://avatars.githubusercontent.com/u/3893481?v=4)](https://github.com/jarrodtomholt "jarrodtomholt (3 commits)")

---

Tags

jarrodtomholtbelongs-to-macro

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jarrodtomholt-belongs-to-macro/health.svg)

```
[![Health](https://phpackages.com/badges/jarrodtomholt-belongs-to-macro/health.svg)](https://phpackages.com/packages/jarrodtomholt-belongs-to-macro)
```

###  Alternatives

[illuminate/database

The Illuminate Database package.

2.8k54.9M11.6k](/packages/illuminate-database)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.5M2](/packages/glushkovds-phpclickhouse-laravel)[lemaur/eloquent-publishing

218.1k1](/packages/lemaur-eloquent-publishing)[laravel-liberu/laravel-gedcom

A package that converts gedcom files to Eloquent models

782.5k1](/packages/laravel-liberu-laravel-gedcom)

PHPackages © 2026

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