Get expert guidance in a few days with a Laravel code review

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

DreamzTech

Hire 6-10+ Yrs. experienced skilled Laravel Developers from DreamzTech. We ensure NDA protected, 100% quality delivery. Contact Us & Discuss Your Need.

DreamzTech
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
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
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
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel