znck/belongs-to-through

This package is abandoned and no longer maintained. The author suggests using the staudenmeir/belongs-to-through package instead.

Adds belongsToThrough relation to laravel models

v2.3.1 2017-07-23 13:11 UTC

README

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 68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f67726164652f3437623437396235383338373432323662383535313237653634333737656132 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.