Giter VIP home page Giter VIP logo

belongs-to-through's Introduction

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 [email protected] instead of using the issue tracker.

Credits

License

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

belongs-to-through's People

Contributors

dannyweeks avatar znck avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.