znck / belongs-to-through
Adds belongsToThrough relation to laravel models
Installs: 204 198
Dependents: 4
Suggesters: 0
Security: 0
Stars: 136
Watchers: 10
Forks: 16
Open Issues: 4
Requires
- illuminate/database: ~5.0
Requires (Dev)
- fabpot/php-cs-fixer: ^1.11
- orchestra/testbench: ~3.0
- phpunit/php-code-coverage: ^3.3
- phpunit/phpunit: ~5.0
- satooshi/php-coveralls: ^1.0
This package is not auto-updated.
Last update: 2019-02-01 14:51:49 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!
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.