Belongs To Through

staudenmeir/belongs-to-through image

Belongs To Through stats

Downloads
3.2M
Stars
934
Open Issues
0
Forks
77

View on GitHub →

Laravel Eloquent BelongsToThrough relationships

BelongsToThrough Laravel Package Summary

BelongsToThrough is a Laravel package designed to facilitate complex database relationships by providing an inverse version of the HasManyThrough relationship. This allows developers to define BelongsToThrough relationships with an unlimited number of intermediate models, enhancing the management of nested relationships in Laravel applications.

Key Features:

  • Inverse Relationships: Enables defining inverse relationships for HasManyThrough, allowing a model to belong to another through one or more intermediate models.
  • Support for Deep Relationships: Supports deep nested relationships through an array of intermediate models.
  • Custom Foreign Keys: Allows specifying custom foreign keys for more tailored database queries.
  • Table Aliases: Supports table aliases to resolve ambiguity when the relationship path includes the same model multiple times (Laravel 6+).
  • Soft Deletes: Handles soft deleted records in relationships and allows including these records using withTrashed().

Installation:

Install the package via Composer:

composer require staudenmeir/belongs-to-through:"^2.5"

For PowerShell users on Windows, use:

composer require staudenmeir/belongs-to-through:"^^^^2.5"

Usage:

After installation, use the BelongsToThrough trait in your model and define relationships as shown below:

Basic Usage:

class Post extends Model
{
use \Znck\Eloquent\Traits\BelongsToThrough;
 
public function country()
{
return $this->belongsToThrough(Country::class, User::class);
}
}

Deep Nested Relationships:

class Comment extends Model
{
use \Znck\Eloquent\Traits\BelongsToThrough;
 
public function country()
{
return $this->belongsToThrough(Country::class, [User::class, Post::class]);
}
}

Handling Soft Deletes:

class Comment extends Model
{
use \Znck\Eloquent\Traits\BelongsToThrough;
 
public function country()
{
return $this->belongsToThrough(Country::class, [User::class, Post::class])
->withTrashed('users.deleted_at');
}
}

Contributing:

Developers are encouraged to contribute to the package by following the guidelines linked in the repository's contributing section.

Credits:

Notable contributions have been made by Rahul Kadyan, Danny Weeks, and other community developers.

BelongsToThrough is a powerful tool for Laravel developers looking to streamline complex database relations in their applications, enhancing both flexibility and maintainability.

staudenmeir photo

Lead Backend Developer: PHP, MySQL, Laravel, Statamic | @laravel Core Contributor

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Staudenmeir Belongs To Through Related Articles

PhpStorm 2026.1 Released image

PhpStorm 2026.1 Released

Read article
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum