Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
zuk3975 committed Aug 21, 2024
0 parents commit 18683f8
Show file tree
Hide file tree
Showing 6 changed files with 666 additions and 0 deletions.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2015 Rahul Kadyan
Copyright (c) 2019 Jonas Staudenmeir

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
150 changes: 150 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# BelongsToThrough

[![CI](https://github.com/staudenmeir/belongs-to-through/actions/workflows/ci.yml/badge.svg)](https://github.com/staudenmeir/belongs-to-through/actions/workflows/ci.yml)
[![Code Coverage](https://scrutinizer-ci.com/g/staudenmeir/belongs-to-through/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/staudenmeir/belongs-to-through/?branch=master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/staudenmeir/belongs-to-through/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/staudenmeir/belongs-to-through/?branch=master)
[![Latest Stable Version](https://poser.pugx.org/staudenmeir/belongs-to-through/v/stable)](https://packagist.org/packages/staudenmeir/belongs-to-through)
[![Total Downloads](https://poser.pugx.org/staudenmeir/belongs-to-through/downloads)](https://packagist.org/packages/staudenmeir/belongs-to-through/stats)
[![License](https://poser.pugx.org/staudenmeir/belongs-to-through/license)](https://github.com/staudenmeir/belongs-to-through/blob/master/LICENSE)

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"

Use this command if you are in PowerShell on Windows (e.g. in VS Code):

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

## Usage

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

```php
class Country extends Model
{
public function posts()
{
return $this->hasManyThrough(Post::class, User::class);
}
}
```

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

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

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

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`):

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

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

You can specify custom foreign keys as the fifth argument:

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

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

### Table Aliases

If your relationship path contains the same model multiple times, you can specify a table alias (Laravel 6+):

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

public function grandparent()
{
return $this->belongsToThrough(
Comment::class,
Comment::class . ' as alias',
null,
'',
[Comment::class => 'parent_id']
);
}
}
```

Use the `HasTableAlias` trait in the models you are aliasing:

```php
class Comment extends Model
{
use \Znck\Eloquent\Traits\HasTableAlias;
}
```

### Soft Deleting

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

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

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

class User extends Model
{
use SoftDeletes;
}
```

## Contributing

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) and [CODE OF CONDUCT](.github/CODE_OF_CONDUCT.md) for details.

## Credits

- [Rahul Kadyan](https://github.com/znck)
- [Danny Weeks](https://github.com/dannyweeks)
- [All Contributors](../../contributors)
35 changes: 35 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "invertus/belongs-to-through",
"description": "Laravel Eloquent BelongsToThrough relationships",
"license": "MIT",
"authors": [
{
"name": "Rahul Kadyan",
"email": "hi@znck.me"
},
{
"name": "Jonas Staudenmeir",
"email": "mail@jonas-staudenmeir.de"
}
],
"require": {
"php": "^8.1",
"illuminate/database": "^10.0"
},
"require-dev": {
"phpunit/phpunit": "^10.1",
"phpstan/phpstan": "^1.10"
},
"autoload": {
"psr-4": {
"Znck\\Eloquent\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
Loading

0 comments on commit 18683f8

Please sign in to comment.