Skip to content
This repository has been archived by the owner on Jun 26, 2024. It is now read-only.

Adds belongsToThrough relation to laravel models

Notifications You must be signed in to change notification settings

TemperWorks/belongs-to-through

 
 

Repository files navigation

Belongs-To-Through

Inverse of HasManyThrough relation is missing from Laravel's ORM. Belongs-To-Through extends Eloquent ORM with belongsToThrough relation.

Eloquent is awesome but it does have some problems. Checkout Plug!

Belongs-To-Through

StyleCI Status Build Status Coverage Status Software License Packagist Latest Version Issues

Installation

Either PHP 5.6+ is required.

To get the latest version of Belongs-To-Through, simply require the project using Composer:

$ composer require znck/belongs-to-through

Instead, you may of course manually update your require block and run composer update if you so choose:

{
    "require": {
        "znck/belongs-to-through": "^2.2"
    }
}

Usage

Within your eloquent model class add following line

class User extends Model {
    use \Znck\Eloquent\Traits\BelongsToThrough;
    ...
}

Example:

Consider a blog application. In this app, a country can have many users and a user can have many articles. So, hasManyThrough provides easy way to access articles from a country.

class Country extends Model {
    use \Znck\Eloquent\Traits\BelongsToThrough;

    public function articles () {
        return $this->hasManyThrough(Article::class, User::class);
    }
}

If we are accessing the country of the article, then we have to use $article->user->country.

Class Article extends Model {
    use \Znck\Eloquent\Traits\BelongsToThrough;

    public function country() {
        return $this->belongsToThrough(Country::class, User::class);
    }
}

Now, the magic: $article->country

Going deeper, City -> District -> State -> Country

Class City extends Model {
	use \Znck\Eloquent\Traits\BelongsToThrough;

	public function country() {
		return $this->belongsToThrough(Country::class, [State::class, District::class]);
	}
}

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email hi@znck.me instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

Adds belongsToThrough relation to laravel models

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%