Coder Social home page Coder Social logo

belongs-to-through's Introduction

Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version Total Downloads License

Introduction

This inverse version of HasManyThrough allows BelongsToThrough relationships with unlimited intermediate models.

Supports Laravel 5.0+.

Installation

composer require staudenmeir/belongs-to-through:"^2.5"

Usage

Consider this HasManyThrough relationship:
Country → has many → User → has many → Post

class Country extends Model
{
    public function posts()
    {
        return $this->hasManyThrough('App\Post', 'App\User');
    }
}

Use the BelongsToThrough trait in your model to define the inverse relationship:
Post → belongs to → User → belongs to → Country

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

    public function country()
    {
        return $this->belongsToThrough('App\Country', 'App\User');
    }
}

You can also define deeper relationships:
Comment → belongs to → Post → belongs to → User → belongs to → Country

Supply an array of intermediate models as the second argument, from the related (Country) to the parent model (Comment):

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

    public function country()
    {
        return $this->belongsToThrough('App\Country', ['App\User', 'App\Post']);
    }
}

You can specify custom foreign keys as the fifth argument:

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

    public function country()
    {
        return $this->belongsToThrough(
            'App\Country',
            ['App\User', 'App\Post'], 
            null,
            '',
            ['App\User' => 'custom_user_id']
        );
    }
}

Soft Deleting

By default, soft-deleted intermediate models will be excluded from the result. Use withTrashed() to include them:

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

    public function country()
    {
        return $this->belongsToThrough('App\Country', ['App\User', 'App\Post'])
            ->withTrashed('users.deleted_at');
    }
}

class User extends Model
{
    use SoftDeletes;
}

Contributing

Please see CONTRIBUTING and CODE OF CONDUCT for details.

Credits

belongs-to-through's People

Contributors

znck avatar staudenmeir avatar dannyweeks avatar axlon avatar andreshg112 avatar wnateg avatar

Watchers

James Cloos 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.